KORN SHELL:
http://www.bolthole.com/solaris/ksh.html
http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html
SYMBOLS:
#!/usr/bin/ksh <--this indicates the shell, which should be used to interpret the script
' ' <--ignores all metacharacters between the quotes (echo '$HOME' --> $HOME) (with ssh variables are interpreted)
" " <--ignores all metacharacters except for $, ` and \ (echo "$HOME" --> /home/root) (with ssh everything is string-literal)
\ <--ignores the special meaning of the following character (echo \$HOME --> $HOME)
(it is the continuation character as well, to continue a command in a new line)
${VAR}hello <--{} distingueshes the variable from the surrounding text
~ <-- represents the home directory of the user (~joe refer's to the home dir of joe)
------------------------------------
x=day
echo there is a ${x}long meeting <--{} is used to eliminate the need of space after the variable
there is a daylong meeting
------------------------------------
x=`date` or x=$(date) <-() is supported only in ksh
------------------------------------
arithmeic:
(( x += 1 )) <- counter adds 1 to x
(( x -= 1 )) <- counter minus 1 from x
x=$(($x+1)) <- counter adds 1 to x
let command can be used for this as well (+,-,*,/):
let x=x+10
let x=x/2
read <- reads from the input to the given variable
export variable=value <- passes variable into a subshell
set -x <- shows real time info for debugging
set -u <- unsets variables
test command:
The test command evaluates the expression and returns a return code 0 if true.
test expression or [ expression ] or [[ expression ]] (leave a space near the squaer braces)
In newer Korn shell the modern notation [[...]] is used, which is an extension of the test command.
integer operations:
if [ $? -eq 0 ] ; then <- equal
if [[ $? -eq 1 ]] <- [[ is for test condition
if [ $? -ne 0 ] ; then <- not equal
if [ $? -lt 0 ] ; then <- less than
if [ $? -gt 0 ] ; then <- greater than
if [ $? -le 0 ] ; then <- less equal
if [ $? -ge 0 ] ; then <- greater equal
$number1 -eq $number2 <- numbers are equal
$number1 -ne $number2 <- numbers are not equal
string operations:
$string1 = $string2 <- strings are identical
$string1 != $string2 <- strings are not identical
if [[ -e $FILE ]] <- if file exist
read command:
It reads one line from standard input and assigns the values of each field to a shell variable.
print command (printf):
printf '%20s\n' "-- PLAYER --"
The syntax is %[-]m[.n]s
%s is the string, [-] is left justified, m is no. of chars wide, [n] is print the first n chars of the string.
The above example shows the string “- PLAYER -” printed, right justified in string wide of 20 chars.
VAR1="Hello"
VAR2="Worldddd"
printf '%10s %-10.5s\n' $VAR1 $VAR2
Hello World
----------------------
RUNNIG A SCRIPT:
Each shell script is executed in a subshell. Variables defined in a shell script cannot be passed back to the parent shell.
. <---if you invoke a shell script with a . (dot) it runs in the current shell.
Variables defined in this script are defined also for the current shell.
./ <- refers to files in the current working directory
~ <- refers to the home directory
3 ways of running a script:
ksh scriptname (read permission needed)
scriptname (read and execute permission needed)(in the PATH variable must be defined the dir)
. scriptname (read permission needed) (it runs in the current shell, a subshell will not be invoked)
$$ process id
$0 shell script name currently executing
$1, $2 ... parameters which were invoked with the script
$* equal all arguments passed
$# number of arguments passed into shell script (if [[ $# -ne 1 ]]...)
$? retrurn code of last command executed
command1 && command2 if command1 successful then do command2 (ls s* && rm s*) (if [[ $ACE = "HIGH" && $banker_score -ge 11 ]])
command1 || command2 if command1 not successful then do command2 ( if [[ $ACE = "HIGH" || $banker_score -ge 11 ]])
exit n pass a return code to shell script
$! process ID of last background process
true this command always returns a true result
false this command always returns a false result
--------------------------------------------------
LOOPS:
for i in `cat file`
do
...
done
for i in /tmp/* it will remove all files in /tmp
do
rm $i
done
while [[ $x -lt 9 ]]
do
...
done
read game?"Another Game (y|n)? "
case $game in
y|Y) :;; <- ":" means continue
n|N) EXT=1;;
esac
--------------------------------------------------
CASE:
(it can happen that there is a problem with numerical variable so this is needed before the CASE condition: "x=echo $x")
case $x in
1 )
echo
;;
2 )
echo "Stopping the ovo agent..."
;;
* )
echo "bye"
;;
esac
--------------------------------------------------
TIPS & TRICKS:
cat | head -n $x | tail -1 <-displays a line from a file
awk 'NR==2' /root/settitle <--displays the 2nd line in the file:/root/setitle
/usr/bin/echo yes | command <-- input is automatically yes
echo "$FS2:\t it is mounted now" <--\t will do a tab in the output
perl -e 'select(undef,undef,undef,.3)' <--it will sleep for 0.3 second
---------------------
Run the script every 2nd Sunday of the month:
DAY=$(date "+%d")
[ $DAY -ge 8 -a $DAY -le 14 ] || exit <- if $DAY greater than 8 and lesser than 14 the script will continue
...rest of the script if not (|| means condition is false) it will exit
it will check if a file older than a week and if yes, delete it:
35 21 * * 0 /usr/bin/find /home/traffic/ -name "trace" -mtime +7 -exec /usr/bin/rm {} \\;
leaving the cursor at the same position while counting:
x=1
while [[ $x -le 100 ]]; do
tput cub 20
print -n $x
(( x += 1 )
done
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11780477/viewspace-1270494/
,如需转载,请注明出处,否则将追究法律责任。