Friday, June 27, 2014

shell script - learning note 2

shell can return value (0: succeed, non-0: fail)
expr 1 + 3 # show 4, because of with space
echo $? $ will show 0

expr 1+ 3 #show syntax error
echo $? $ will show non-0, here 2 in fact.

read: almost same as Fortran.
Example:
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"


Wildcard (shorthand) 未知数,通配符
* means any string or group of characters.
? means any single character.
[xxx] Matches any one of the enclosed characters.

two command or more in one line
Example: data;who;cal

Some useful command:
tail file # print last 10 lines of the file.

diamond (i.e. <>)symbol
Example: sort < sname > sorted_names

Pipe: a way to connect the output of one program to the input of another program without any temporary file.
Example: who | sort 
                  ls -l | wl -l

Loop
Bash supports: for loop and while loop
Example: 
for ((  i = 0 ;  i <= 5;  i++  )) # WATCH OUT THE DOUBLE PARENTHESIS. There is no reason, you have to use it this way.
do
  echo "Welcome $i times"
done

  

No comments:

Post a Comment