Shell 学习笔记 (org-mode制作)

 

S shell learning note v1.0

1 what is shell

shell is the middle man of kernel and user.

  1. to show shell installed in the system. different shell may has differnet grammar

    cat /etc/shells
    
  2. show current shell used.

    echo $SHELL
    

2 hello world program

 

2.1 src

#!/bin/sh
echo "hello shell"

2.2 add run privilege

$ sudo chmod +x ./helloshell.sh

2.3 run shell

./helloshell.sh

3 variables in shell

variables in shell are system variables and user defined variables.

3.1 system variables

use

$ set

to show the system variables.

3.2 user defined variables

 
3.2.1 define and use

variables in shell have no data type. shell will automatically transfer the type. e.g.

$ msg="hello world"

Attention: there is no blank spaces before and after '='

3.2.2 local vs global variable

global shell variable stored in ~/.bashrc, otherwise, they are all local shell variables.

4 control structure

 

4.1 condition judgement

use test or [] to judge a condition in shell.

4.1.1 math operation judgement
seqexampleexplain
1a -eq ba == b
2a -ne ba != b
3a -lt ba < b
4a -le ba <= b
5a -gt ba > b
6a -ge ba >= b
4.1.2 string comparation
seqexampleexplain
1str1 = str2str1 == str2
2str1 != str2str1 != str2
3str1str1 not null or not defined.
4.1.3 logic judgement
seqexampleexplain
1!not
2exp1 -a exp2exp1 && exp2
3exp1 -o exp2exp1 or exp2
4.1.4 if-else
#!/bin/sh

if [ $# -ne 1 ]; then
    echo "$0 : you must give one integer"
    exit 1
fi

if [ $1 -gt 0 ]; then
    echo "$1 is positive"
else
    echo "$1 is negative"
fi
4.1.5 for
#!/bin/sh

for i in 1 2 3 4 5
do
    echo "welcome $i"
done
4.1.6 while
#!/bin/sh

# test while loop

if [ $# -ne 1 ]; then
    echo "usage: $0 integer"
    exit 1
fi

n=$1
i=0
while [ $i -le 10 ]
do
    echo "$n * $i = `expr $i \* $n`"
    i=`expr $i + 1`
done
4.1.7 case
#!/bin/sh

# test case statement

if [ $# -ne 1 ]; then
    echo "usage: $0 [a|b|c]"
    exit 1
fi

case $1 in
    "a" )
        echo "a select"
        ;;
    "b" )
        echo "b select"
        ;;
    "c" )
        echo "c select"
        ;;
    * )
        echo "no action"
        ;;
esac

5 fucntion

#!/bin/sh

demo()
{
    echo "all function args: $*"
    echo "the first arg : $1"
    echo "the second arg : $2"
    echo "the third arg : $3"
}

# call the function
demo -f foo bar

exit 0

6 debug

 

6.1 echo

use echo to info you.

6.2 sh -v ./shell or sh -x ./shell

$ sh -v ./shell print the content shell get

$ sh -x ./shell show the command then print it.

Created: 2015-07-23 Thu 22:20

Emacs 24.4.1 (Org mode 8.3beta)

Validate

转载于:https://www.cnblogs.com/aqing1987/p/4671983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值