linux shell scrip(脚本)编程简介------变量、特殊符号和表达式

       一些朋友从事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]$ 
     注意: =两边绝对不可以有空格, shell script 居然要这么要求, 不知道当时设计人员是怎么想的, 故意要为难人么?

 

 

 

      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

 
 
 
     4.  双引号, 单引号, 倒引号(位于Esc下面), \号和{}的作用, 看程序:
[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]$
    不多解释了, 一切尽如上。
 
 
    5. 表达式求值
[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
     注意:$[ 1 + 2 ]中, 1, +, 2的两侧也必须有空格。 expr 1 + 2中, 1, +, 2的两侧也必须有空格。 而let那里又不能, 好恶心啊。
 
     实际上, 还有一种 更好的计算表达式的方法, 用$((xxxxx)), 其中xxxxxx可以直接用C/C++形式的表达式了, 比如i++( 感谢网友KISS Gentoo对我的提醒和帮助得意), 如下:
[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]$
 
 
     本文先来进行一个简单的热身, 后续会介绍更多有关linux shell script的内容。 最后, 欣赏一个小小的shell script程序:
[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]$

 
 
 
 
 
 
 
 

 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值