2012年8月28日火曜日

5.1.4.1. String comparison

Table 5-1. String comparison operators
Operator True if...
str1 = str2[4] str1 matches str2
str1 != str2 str1 does not match str2
str1 < str2 str1 is less than str2
str1 > str2 str1 is greater than str2
-n str1 str1 is not null (has length greater than 0)
-z str1 str1 is null (has length 0)

5.1.4.2. File attribute checking

Table 5-2. File attribute operators
Operator True if...
-a file file exists
-d file file exists and is a directory
-e file file exists; same as - a
-f file file exists and is a regular file (i.e., not a directory or other special type of file)
-r file You have read permission on file
-s file file exists and is not empty
-w file You have write permission on file
-x file You have execute permission on file, or directory search permission if it is a directory
-N file file was modified since it was last read
-O file You own file
-G file file 's group ID matches yours (or one of yours, if you are in multiple groups)
file1 -nt file2 file1 is newer than file2 [6]
file1 -ot file2 file1 is older than file2

[6] Specifically, the -nt and -ot operators compare modification times of two files.

$* and $@

"$*" is a single string that consists of all of the positional parameters, separated by the first character in the value of the environment variable IFS (internal field separator), which is a space, TAB, and NEWLINE by default. On the other hand, "$@" is equal to "$1" "$2"... "$ N", where N is the number of positional parameters. That is, it's equal to N separate double-quoted strings, which are separated by spaces. If there are no positional parameters, "$@" expands to nothing. We'll explore the ramifications of this difference in a little while.

5.2. for

for x := 1 to 10 do
begin
    statements...
end
 
for name  [in list ]
do
    statements that can use  
    $name... 
done 

5.1.5. Integer Conditionals

Table 5-3. Arithmetic test operators
Test Comparison
-lt Less than
-le Less than or equal
-eq Equal
-ge Greater than or equal
-gt Greater than
-ne Not equal

2012年8月27日月曜日

Task 4-2


Task 4-2

You are writing a graphics file conversion utility for use in creating a web page. You want to be able to take a PCX file and convert it to a JPEG file for use on the web page.[7]

${...}, $(...)

${...}:  String Operator
  • String Substitution
    •  
  • String Pattern Matching
  • String Length
$(...):  Command Substitution
Here are some simple examples:
  • The value of $(pwd) is the current directory (same as the environment variable $PWD).
  • The value of $(ls $HOME) is the names of all files in your home directory.
  • The value of $(ls $(pwd)) is the names of all files in the current directory.
  • The value of $(< alice) is the contents of the file alice with any trailing newlines removed.[10]
    [10] Not available in versions of bash prior to 2.02.
  • To find out detailed information about a command if you don't know where its file resides, type ls -l $(type -path -all command-name). The -all option forces type to do a pathname look-up and -path causes it to ignore keywords, built-ins, etc.
  • If you want to edit (with vi) every chapter of your book on bash that has the phrase "command substitution," assuming that your chapter files all begin with ch, you could type:
          vi $(grep -l 'command substitution' ch*)

  • The -l option to grep prints only the names of files that contain matches.