Using flags on the command line

Perhaps you haven't realized it yet, but most commands you use on the command line use a combination of positional arguments and flags. The most basic command in Linux, cd, uses a single positional argument: the directory you want to move to.

It does actually have two flags that you can use as well: -L and -P. The purpose of these flags is niche and not worth explaining here. Almost all commands use both flags and positional arguments complementarily.

So then, when do we use which? As a rule of thumb, flags are often used for modifiers, while positional arguments are used for targets. A target is simple this is: the thing you want to manipulate with the command. In the case of ls, this means that the positional arguments are the files or directories that should be listed (manipulated) by the command.

For the ls -l /tmp/ command, /tmp/ is the target, and -l is the flag used to modify the behavior of ls. By default, ls lists all files without extra information such as ownership, permissions, size, and so on. If we want to modify the behavior of ls, we add one or more flags: -l tells ls to use the long listing format, which prints each file on its own line, and prints the extra information about the file too.

Do note that between ls /tmp/ and ls -l /tmp/, the target does not change, but the output does, since we modified it with the flag!

Some flags are even more special: they require their own positional argument! So not only can we use the flag to modify the command, but the flag itself has multiple options for how to modify the command's behavior.

A good example of this is the find command: by default, it finds all files within a directory, as follows:

reader@ubuntu:~/scripts/chapter_14$ find
.
./reverser-crontab
./wall.txt
./base-crontab
./date-redirection-crontab

Alternatively, we can use find with a positional argument to search not in the current working directory, but somewhere else, as follows:

reader@ubuntu:~/scripts/chapter_14$ find ../chapter_10
../chapter_10
../chapter_10/error.txt
../chapter_10/grep-file.txt
../chapter_10/search.txt
../chapter_10/character-class.txt
../chapter_10/grep-then-else.sh

Now, find also allows us to use the -type flag to only print files of a certain type. But by only using the -type flag, we have not yet specified what file type we want to print. By specifying the file type directly after the flag (ordering is critical here), we tell the flag what to look for. It looks something like the following:

reader@ubuntu:/$ find /boot/ -type d
/boot/
/boot/grub
/boot/grub/i386-pc
/boot/grub/fonts
/boot/grub/locale

Here we looked for a type of d (directory) within the /boot/ directory. Other arguments to the -type flag include f (file), l (symbolic link), and b (block device).

As always, ordering is important, and something like this will happen if you do not get it right:

reader@ubuntu:/$ find -type d /boot/
find: paths must precede expression: '/boot/'
find: possible unquoted pattern after predicate '-type'?

Unfortunately for us, not all commands are created equal. Some are more forgiving on the user, and try their hardest to make sense of what has been given as input. Others are much more strict: they will run whatever is passed, even if it does not make any functional sense. Always make sure you verify whether you are using the command and its modifiers correctly!

The preceding examples use flags differently to how we'll learn to use them with getopts. These examples should only serve to illustrate the concepts of script arguments, flags, and flags-with-arguments. These implementations are written without the use of getopts and thus do not map precisely to what we'll be doing later.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.185