一些朋友从事linux相关的维护工作, 这自然需要会搞shell scipt编程。另外一些朋友从事linux相关的测试工作,经常涉及到一些自动化测试的东西, 当然需要会点shell scrip编程啊。 还有很多朋友从事linux相关的开发工作, 那么, 我要说, 不会shell script, 那是不合格的, 尽管有很多程序员跟我说: 我只要看得懂即可, 会不会写没有关系。
先说说linux命令, 如果你连这个都不知道, 那也没有关系, 建议你直接关闭网页, 不要再往下看看了, 免得浪费你的时间。 那什么是shell script呢? 翻译过来, 就是脚本啦。 我认为, linux shell script很简单很简单, 其本质是:linux命令的集合(实现批处理)。 有的大牛会补充说, shell script不只是linux命令的集合啊, 还有分支循环等控制语句啊。 我要说, 这个补充是多余的,是细节, 而不是本质。
学习shell script, 并不要求有什么编程知识, 当然, 如果你有c/c++的简要编程基础, 那就更好了。 学习shell script, 并需需要有gcc, 因为shell script是命令的集合, 不需要编译链接。 在本文中, 我们来学习一下shell script的变量、特殊符号和表达式
1. 最简单的shell script程序
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
echo -------------------------------------
echo "taoge is learning linux shell script"
echo "-------------------------------------"
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh
-------------------------------------
taoge is learning linux shell script
-------------------------------------
[taoge@localhost learn_shell]$
注意:shell script程序会一行一行地执行。 echo命令最后会自动加上一个换行符, 所以我们就不用添加了。
这样, 你基本就入门shell script了。
2. 变量的理解, 我们在前几篇中已经详细介绍过, 所以就不讲了, 直接看程序:
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
x="good boy" #define x
echo $x # show the value of x
echo $y # show the value of y
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh
good boy
[taoge@localhost learn_shell]$
3. shell script的命令行参数。 学过c/c++的人都知道, main还有输入参数呢(argc, argv). 下面我们看程序:
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
echo $0 # the name of shell script file
echo $1 # first parameter
echo $2 # second parameter
echo -----------------------------------
echo $* # all parameters
echo $@ # equivalent to $*
echo $# # the number of all paramerters
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh
./a.sh
-----------------------------------
0
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh one two three four five
./a.sh
one
two
-----------------------------------
one two three four five
one two three four five
5
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
x="good afternoon"
echo 0 $x
echo "1, value x is $x"
echo '2, display $x'
echo "3. date is `date`, are you clear?"
echo "4, display \$x"
echo "5, $xeveryone"
echo "6, ${x}everyone"
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh
0 good afternoon
1, value x is good afternoon
2, display $x
3. date is Fri May 8 23:52:27 PDT 2015, are you clear?
4, display $x
5,
6, good afternooneveryone
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ vim a.sh
[taoge@localhost learn_shell]$ clear
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
sum=1+2
echo $sum # still 1+2, not 3
a=1
b=$a+2
echo $b # still 1+2, not 3
result1=$[ 1 + 2 ]
echo $result1 # 3
expr 1 + 2 # calculate and display
echo --------------
let result2=1+2
echo $result2 # 3
last=$[ 2#101 + 2 ] # 7
echo $last
[taoge@localhost learn_shell]$ ./a.sh
1+2
1+2
3
3
--------------
3
7

[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
x=5
y=6
z=$((x+y))
echo $z
z=$((x + y))
echo $z
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ cat a.sh
#! /bin/bash
for x in $@
do
rm -rf $x
mkdir $x
touch $x/test.txt
done
for x in $@
do
ls -l $x
done
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$
[taoge@localhost learn_shell]$ ./a.sh
[taoge@localhost learn_shell]$ ./a.sh 1 2 3
total 0
-rw-rw-r-- 1 taoge taoge 0 May 9 00:56 test.txt
total 0
-rw-rw-r-- 1 taoge taoge 0 May 9 00:56 test.txt
total 0
-rw-rw-r-- 1 taoge taoge 0 May 9 00:56 test.txt
[taoge@localhost learn_shell]$