Link Search Menu Expand Document

Command Line Interview Questions (Advanced)

How would you count every occurrence of the term "potato" in all the files appearing under the current directory and its subdirectories, recursively?

To list all "potato" occurrences on a separate line, a grep -o potato is required. Adding the r flag to the command processes all files under the specified direction reciprocally, and the I flag guarantees that matches are missed in the binary files. Furthermore, the w flag is used to follow the exact word and disregard the superstrings as "potatoes," and the I flag may also be applied to make the search case-insensitive:

The number of lines that this grep command provides is the number of the requested term events that can be counted in the wc-l command.

How to swap out stdout and stderr of a command?

A third file descriptor is created to swap stdout and stderr from one command. to the same objective as stderr (referenced by &2). (referenced by &2). Stderr is then guided to the same goal stdout (&1). Finally, stdout is indicated where the current descriptor is shown (which was originally pointed out by the same stderr target).

How can you write a shell script, which prints in reverse order all the additional arguments?

For instance, $0 is the script's name, $1 is the first additional argument, $2 the second argument is the second, and so on. The total amount of other ideas is contained in $#. It means that you have the same number of arguments as $#.

Any additional statement can be printed reversed using a loop that begins at $# and finishes at 1.

How could a shell script be written and ensured that only one instance of the hand might run for every user? Strong atomicity is not required.

In Bash:
LOCKFILE=/tmp/lock-`whoami`
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "Already running!"
    exit 1
fi
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

Begin by defining a lock file name. The lock file will be created if the user name of the current user is suffixed. Check then if the lock file is open and whether the PID is running in the lock file. If so, leave the post.

Build an unclean outfit trap to delete the lock file (any exit with the signal INT or TERM).

If you have not yet removed the script, build and store the lock file ($) in the current script method PID.

What is shared, slave, private, and unbindable mountpoints?

A shared mounting point can be repeated as many times as possible, and each copy is always the same. It is repeated in all other mount dots, some mount points occurring under a shared mount point in some subdirectory.

A slave mount point is very similar to a shared mount point, except that information about a mount point is transmitted in one direction. Only mount and unmount events are obtained at a slave mount stage. Anything mounted at this mirrored mounting point would not shift to the original mounting point.

The name means a private mount point: personal. Other replicated mount points mounts that occur under a remote mount point will not be shown elsewhere unless they are also specifically mounted. An unbinding mounting stage, which is also private by itself, is not repeated anywhere else.

Other useful articles:


Back to top

© , CMD Windows — All Rights Reserved - Terms of Use - Privacy Policy