Shell script之if...then

时间:2021-10-16 19:57:22

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 # ----------------------------------------------------------------------------- if [ $# -eq ]; then
here=$PWD;
cd '../../02_modelsim'  # work directory in your project
vsim -gui&
cd $here;
elif [ $ == "-h" ]; then
12 # questasim-doc:
evince /Software/AMS/current/questasim/v10.3b/docs/pdfdocs/_bk_questa_sim.pdf &
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`