Shell Scrips

please refer:

Condition

If

if [ condition1 ]; then

elif [ condition2 ]

else

fi

case

case $var in
    'value1' )
        # ...
        ;;
    'value2' )
        # ...
        ;;
esac

for

refer: http://www.cyberciti.biz/faq/bash-for-loop/

for VARIABLE in file1 file2 file3
do
  # echo $VARIABLE
done

for VARIABLE in $(ls /tmp/)
do
  # echo $VARIABLE
done


for i in {1..5}
do
   # echo "Welcome $i times"
done

# $INCREMENT deault = 1
for i in $(seq $BEGIN $INCREMENT $END)
do
   echo "get $i "
done


for (( c=1; c<=5; c++ ))
do
   echo "Welcome $c times"
done


for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

while

while [ condition ] 
do
  # ...
done

Computation

var=$((運算內容))

num=$(( 13 % 3 ))

Input

argv

$# : the num of argv

echo $0 $1 $2

read

read -p "Are you crazy? [Y/N]:" answer

getopts

Refer: http://wiki.bash-hackers.org/howto/getopts_tutorial

while getopts ":u:p:h" opt; do
    case $opt in
        u)
            USER=$OPTARG
            ;;
        p)
            PASSWORD=$OPTARG
            ;;
        h)
            echo "./$0 -u <name> -p <password>"
            exit 1
            ;;
        \?)
            echo "Invalid option: -$OPTARG"
            echo "./$0 -u <name> -p <password>"
            exit 1
            ;;
    esac
done

Output Log

refer:

operator notes
0 標準輸入
1 標準輸出
2 標準錯誤
usage notes
1>filename Redirect stdout to file "filename."
2>filename Redirect stderr to file "filename."
&>filename Redirect both stdout and stderr to file "filename."
2>&1 Redirects stderr to stdout.
exec 3>&1 1>>${LOG_FILE} 2>&1
would send stdout and stderr output into the log file, but would also leave you with fd 3 connected to the console, so you can do

echo "Some console message" 1>&3
to write a message just to the console, or

echo "Some console and log file message" | tee /dev/fd/3
to write a message to both the console and the log file - tee sends its output to both its own fd 1 (which here is the LOG_FILE) and the file you told it to write to (which here is fd 3, i.e. the console).

Example:

exec 3>&1 1>>${LOG_FILE} 2>&1

echo "This is stdout"
echo "This is stderr" 1>&2
echo "This is the console (fd 3)" 1>&3
echo "This is both the log and the console" | tee /dev/fd/3
would print

This is the console (fd 3)
This is both the log and the console
on the console and put

This is stdout
This is stderr
This is both the log and the console
into the log file.

run command on background

use wait to wait for all command run finish

command1 &
command2 &
wait

String Handling

Divide String

${MYVAR#pattern}       # delete shortest match of pattern from the beginning
${MYVAR##pattern}      # delete longest match of pattern from the beginning
${MYVAR%pattern}       # delete shortest match of pattern from the end
${MYVAR%%pattern}      # delete longest match of pattern from the end

Bash setting

Usage:

  • set [-o option-name] [arg ...] : set env
  • set [+o option-name] [arg ...] : unset env
Option-name Means
-e Exit immediately if a pipeline or, a subshell command enclosed in parentheses
-x After expanding each command and display the expanded value

Function

returning value from called function

refer: http://stackoverflow.com/questions/8742783/returning-value-from-called-function-in-shell-script

A bash function can't return a string directly like you want it to. You can do three things:

echo a string return an exit status, which is a number, not a string share a variable This is also true for some other shells.

Here's how to do each of those options:

1.) echo strings

lockdir="somedir"
testlock(){
    retval=""
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval="true"
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval="false"
    fi
    echo "$retval"
}

retval=$( testlock )
if [ "$retval" == "true" ]
then
     echo "directory not created"
else
     echo "directory already created"
fi

2.) return exit status

lockdir="somedir"
testlock(){
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval=0
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval=1
    fi
    return "$retval"
}

testlock
retval=$?
if [ "$retval" == 0 ]
then
     echo "directory not created"
else
     echo "directory already created"
fi

3.) share variable

lockdir="somedir"
retval=-1
testlock(){
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval=0
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval=1
    fi
}

testlock
if [ "$retval" == 0 ]
then
     echo "directory not created"
else
     echo "directory already created"
fi

results matching ""

    No results matching ""