Название | Linux Bible |
---|---|
Автор произведения | Christopher Negus |
Жанр | Зарубежная компьютерная литература |
Серия | |
Издательство | Зарубежная компьютерная литература |
Год выпуска | 0 |
isbn | 9781119578895 |
-e
file
-a)
-f
file
-g
file
-h
file
-L)
-k
file
-L
file
-n
string
-O
file
-p
file
-r
file
-s
file
-S
file
-t
fd
-u
file
-w
file
-x
file
-z
string
-a
expr2
-o
expr2
-nt
file2
-ot
file2
-ef
file2
=
var2
-eq
var2
-ge
var2
-gt
var2
-le
var2
-lt
var2
!=
var2
-ne
var2
There is also a special shorthand method of performing tests that can be useful for simple one-command actions. In the following example, the two pipes (||
) indicate that if the directory being tested for doesn't exist (-d dirname
), then make the directory (mkdir $dirname
):
# [ test ] || action # Perform simple single command if test is false dirname="/tmp/testdir" [ -d "$dirname" ] || mkdir "$dirname"
Instead of pipes, you can use two ampersands to test if something is true. In the following example, a command is being tested to see if it includes at least three command-line arguments:
# [ test ] && {action} # Perform simple single action if test is true [ $# -ge 3 ] && echo "There are at least 3 command line arguments."
You can combine the &&
and ||
operators to make a quick, one-line if…then…else
. The following example tests that the directory represented by $dirname
already exists. If it does, a message says the directory already exists. If it doesn't, the statement creates the directory:
# dirname=mydirectory [ -e $dirname ] && echo $dirname already exists || mkdir $dirname
The case command
Another frequently used construct is the case
command. Similar to a switch
statement in programming languages, this can take the place of several nested if
statements. The following is the general form of the case
statement:
case "VAR" in Result1) { body };; Result2) { body };; *) { body } ;; esac
Among other things, you can use the case
command to help with your backups. The following case
statement tests for the first three letters of the current day (case 'date +%a' in
). Then, depending on the day, a particular backup directory (BACKUP
) and tape drive (TAPE
) are set.
# Our VAR doesn't have to be a variable, # it can be the output of a command as well # Perform action based on day of week case `date +%a` in "Mon") BACKUP=/home/myproject/data0 TAPE=/dev/rft0 # Note the use of the double semi-colon to end each option ;; # Note the use of the "|" to mean "or" "Tue" | "Thu") BACKUP=/home/myproject/data1 TAPE=/dev/rft1 ;; "Wed" | "Fri") BACKUP=/home/myproject/data2 TAPE=/dev/rft2 ;; # Don't do backups on the weekend. *) BACKUP="none" TAPE=/dev/null ;; esac
The asterisk (*
) is used as a catchall, similar to the default
keyword in the C programming language. In this example, if none of the other entries are matched on the way down the loop, the asterisk is matched and the value of BACKUP
becomes none
. Note the use of esac
, or case
spelled backwards, to end the case
statement.
The ″for…do″ loop
Loops