Shell script之if...then

1  Variable in shell 

  If you want to print variable, then use echo command.

  In front of the variable to add $variabel or use ${variable} to fetch the value of variable.

# echo $variable
# echo $PATH

2  if....then 

if condition
then
    condition is true
    execute all commands up to else statement
else
    if condition is not true then
    execute all commands up to fi
fi

3  Example 

 1 #!/bin/bash
 2 # ----------------------------------------------------------------------------
 3 # call pythonscript, then modelsim
 4 # -----------------------------------------------------------------------------
 5 
 6 if [ $# -eq 0 ]; then
 7     here=$PWD;
 8     cd '../../02_modelsim'  # work directory in your project
 9     vsim -gui&
10     cd $here;
11 elif [ $1 == "-h" ]; then
12     # questasim-doc: 
13     evince /Software/AMS/current/questasim/v10.3b/docs/pdfdocs/_bk_questa_sim.pdf &
14 fi

4  Explanation

`$` refer to `value of` and
`#` refer to `number of / total number`

So together

`$#` refer to `The value of the total number of command line arguments passed.`

Thus, you can use $# to check the number of arguments/parameters passed like you did and handle any unexpected situations.

`$1` for `value of 1st argument passed`
`$2` for 'value of 2nd argument passed`
原文地址:https://www.cnblogs.com/mengdie/p/4366536.html