一个简单的脚本
脚本可以不会写,但是要会看,会改,当然会写最好啦
#!/bin/bash
name="Linux"
name1="My name is $name"
name1='My name is $name'
echo $name1
echo $name2
名为test.sh 执行 sh test.sh,第一行声明使用的 shell。(PS:顺便看下单引号和双引号的区别)
运算符declare
- shell中定义的变量默认都是字符串形式,如a=3;b=5;c=$a*$5;echo $c 得到的结果为3*5;那么久需要declare声明变量
- declare [-afirx] (a:数组;f:函数;i:整数;r:只读;x:通过环境输出变量)
- 再来看看一个脚本程序
#!/bin/bash
num1=2*3+5*13-32
declare -i num2=2*3+5*13-32
echo $num1
echo $num2
交互式脚本
简单的说交互式脚本就是程序会根据您输入的数据进行判断。最简单的是read,下面一个简单的例子
#!/bin/bash
echo "input your name"
read name
echo "your name is $name"
现在说说怎么定义一个脚本的参数代号(就是说输入参数有多个,怎么区别第几个)?例:
#!/bin/bash
echo "the scripts is $0"
echo "parameters $1 $2 $3"
运行:sh test.sh pa1 pa2 pa3
脚本逻辑判断式与表达式
在脚本里很重要的一项工作就是判断是否可行,如:当我们要建立以个目录时,得先判断该目录是否存在……
逻辑判断式与if…then…fi的关系密不可分,下面看看
条件判断
- if…then…fi
最常用的是if … then … else if … then … end if 语句,下面看一个例子
#!/bin/bash
echo "input y\n"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo "script is running"
else
echo "STOP!"
fi
需要注意的点:
1. 在[]中,只能有一个判断式
2. 在[] 与[]间,可以用&&或||结合判断式
3. 每一个独立的组件之间需要用空格键隔开([(空格)”$yn”(空格)=(空格])”y”(空格))
再来看下用参数表示
#!/bin/bash
if [ "$1" = "hello" ]; then
echo "hello Linux"
elif [ "$1" = "" ]; then
echo "you must input parameters"
else
echo "Bye Bye"
fi
现在再看一个长一点的程序(规范点儿的程序),用于检测端口是否开启
!/bin/bash
#program:Using to study the [if ... then ... fi] program
#written by: xxx
#date:2015/10/05
#content:i wiil use this program to show your service
#1. print the program's work in your screen
echo "Now the services of your Linux system will be detect"
echo "The www, sshwill be detect"
echo " "
#2. www
www='netstat -an|grep LISTEN|grep :80'
if [ "$www" != "" ]; then
echo "WWW is running"
else
echo "WWW is not runniing"
fi
#3. ssh
ssh='netstat -an|grep LISTEN|grep :22'
if [ "$ssh" != "" ]; then
echo "SSH is running"
else
echo "SSH is not runniing"
fi
- case… esac
直接看程序
#!/bin/bash
echo "this program will select your selection"
case $1 in
one)
echo "your choice is one"
;;
two)
echo "your choice is one"
;;
three)
echo "your choice is one"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac
换成交互式的,如下:
#!/bin/bash
echo "press your select one two three"
read num
case $num in
one)
echo "your choice is one"
;;
two)
echo "your choice is one"
;;
three)
echo "your choice is one"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac
循环
- 语句
- for ((条件1; 条件2; 条件3))
- for variable in variable1 variable2
- while [condition1] && { || } [condition2] …
- until [condition2] && { || } [condition2]…
来看一个for的程序
#!/bin/bash
declare -i s
for (( i=1; i<=100; i=i+1))
do
s=s+i;
done
echo "sum = $s"
下面是while 版本
#!/bin/bash
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i;
i=i+1;
done
echo "sum is $s"
下面是until 版本
#!/bin/bash
declare -i i
declare -i s
until [ "$i" = "101" ]
do
s=s+i;
i=i+1;
done
echo "sum is $s"
下面看下for另一种循环方式用于非数字
#!/bin/bash
LIST="David Lili Tom"
for i in $LIST
do
echo $i
done
再来看一个例子,输出主机账号
#!/bin/bash
account=`cut -d ":" -f1 /etc/passwd | sort`
for i in $account
do
echo $i
done
注意上述是反引号,而不是单引号
下面看一个程序判断有没有文件(-e判断)或该文件是目录还是文件(-d,-f),就用当前目录下的test看试验下吧
#!/bin/bash
if [ ! -e test ]; then
touch test
echo "just make a file test"
exit 1
elif [ -e test ] && [ -f test ]; then
rm test
mkdir test
echo "remove file ==> test"
echo "and make dir test"
exit 1
elif [-e test ] && [ -d test ]; then
rm -rf test
echo "remove dir test"
exit 1
else
echo "does here has anything?"
fi
调试脚本
语法:sh [-nvx] scripts
-n : 不执行脚本,查询脚本内的语法,若有错误则列出
-v : 执行脚本之前,先将脚本的内容显示在屏幕上
-x : 将用到的脚本内容显示在屏幕上,与 -v稍微不同
关于
shell脚本程序应该多学习,多看,多模仿,这里仅仅是初学习shell 入门知识
参考 《鸟哥的Linux私房菜》