您的位置:首页 > 运维架构 > Shell

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

2015-07-23 22:32 525 查看


S shell learning note v1.0

Table of Contents

1. what is shell

2. hello world program

2.1. src

2.2. add run privilege

2.3. run shell

3. variables in shell

3.1. system variables

3.2. user defined variables

3.2.1. define and use

3.2.2. local vs global variable

4. control structure

4.1. condition judgement

4.1.1. math operation judgement

4.1.2. string comparation

4.1.3. logic judgement

4.1.4. if-else

4.1.5. for

4.1.6. while

4.1.7. case

5. fucntion

6. debug

6.1. echo

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

1 what is shell

shell is the middle man of kernel and user.

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

cat /etc/shells


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: