码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-07-23 23:22:40      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 

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
1 a -eq b a == b
2 a -ne b a != b
3 a -lt b a < b
4 a -le b a <= b
5 a -gt b a > b
6 a -ge b a >= b

4.1.2 string comparation

seqexampleexplain
1 str1 = str2 str1 == str2
2 str1 != str2 str1 != str2
3 str1 str1 not null or not defined.

4.1.3 logic judgement

seqexampleexplain
1 ! not
2 exp1 -a exp2 exp1 && exp2
3 exp1 -o exp2 exp1 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

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

标签:

原文地址:http://www.cnblogs.com/aqing1987/p/4671983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!