Linux Bible. Christopher Negus

Читать онлайн.
Название Linux Bible
Автор произведения Christopher Negus
Жанр Зарубежная компьютерная литература
Серия
Издательство Зарубежная компьютерная литература
Год выпуска 0
isbn 9781119578895



Скачать книгу

Is the file a directory? -e file Does the file exist? (same as -a) -f file Does the file exist, and is it a regular file (for example, not a directory, socket, pipe, link, or device file)? -g file Does the file have the set group id (SGID) bit set? -h file Is the file a symbolic link? (same as -L) -k file Does the file have the sticky bit set? -L file Is the file a symbolic link? -n string Is the length of the string greater than 0 bytes? -O file Do you own the file? -p file Is the file a named pipe? -r file Is the file readable by you? -s file Does the file exist, and is it larger than 0 bytes? -S file Does the file exist, and is it a socket? -t fd Is the file descriptor connected to a terminal? -u file Does the file have the set user id (SUID) bit set? -w file Is the file writable by you? -x file Is the file executable by you? -z string Is the length of the string 0 (zero) bytes? expr1 -a expr2 Are both the first expression and the second expression true? expr1 -o expr2 Is either of the two expressions true? file1 -nt file2 Is the first file newer than the second file (using the modification time stamp)? file1 -ot file2 Is the first file older than the second file (using the modification time stamp)? file1 -ef file2 Are the two files associated by a link (a hard link or a symbolic link)? var1 = var2 Is the first variable equal to the second variable? var1 -eq var2 Is the first variable equal to the second variable? var1 -ge var2 Is the first variable greater than or equal to the second variable? var1 -gt var2 Is the first variable greater than the second variable? var1 -le var2 Is the first variable less than or equal to the second variable? var1 -lt var2 Is the first variable less than the second variable? var1 != var2 Is the first variable not equal to the second variable? var1 -ne var2 Is the first variable not equal to the second variable?

       # [ 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