转自http://blog.csdn.net/jb19900111/article/details/13507815
自己写了一下小的shell实例,虽然很小,但所有的大的程序都是由小的模块堆积起来的,程序员一定要懂得一种脚本的书写,而我,只会在Linux下工作,所以就只能写linux的shell脚本了,呵呵,本文会陆续更新,给自己加油!
1.模拟linnux登录shell
[plain] view plain
copy
print ?
- #!/bin/bash ##! /bin/sh 是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面根的是此解释此脚本的shell的路径。
- echo -n "login:" #echo -n 不换行输出
- read name #read命令接收标准输入(键盘)的输入,或者其他文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。
- #在上面read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量。如果输入数据个数过多,则最后剩下的所有值都给最后一个变量,如果太少输入不会结束。
- echo -n "password:"
- read passwd
- if [ $name = "cht" -a $passwd = "abc" ];then #使用一个定义过的变量,只要在变量名前面加美元符号($),
=
判断字符串是否相等,[ .... -a ..... ] 相当于 "与" -o 表示 或
- echo "the host and password is right!"
- else echo "input is error!"
- fi
#!/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "cht" -a $passwd = "abc" ];then
echo "the host and password is right!"
else echo "input is error!"
fi
2.比较两个数大小
[plain] view plain
copy
print ?
- #!/bin/bash
- echo "please enter two number"
- read a
- read b
- if test $a -eq $b #test 整数1 –eq 整数2 整数相等
- then echo "NO.1 = NO.2"
- elif test $a -gt $b #test 整数1 –gt 整数2 整数1大于整数2
- then echo "NO.1 > NO.2"
- else echo "NO.1 < NO.2"
- fi
#!/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 < NO.2"
fi
3.查找/root/目录下是否存在该文件
[plain] view plain
copy
print ?
- #!/bin/bash
- echo "enter a file name:"
- read a
- if test -e /root/$a #test –e File 文件是否存在 (常用)
- then echo "the file is exist!"
- else echo "the file is not exist!"
- fi
#!/bin/bash
echo "enter a file name:"
read a
if test -e /root/$a
then echo "the file is exist!"
else echo "the file is not exist!"
fi
4.for循环的使用
[plain] view plain
copy
print ?
- #!/bin/bash
- clear #刷新屏幕
- for num in 1 2 3 4 5 6 7 8 9 10 #列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量num
- do
- echo "$num"
- done
#!/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo "$num"
done
5..查看是否当前用户
[plain] view plain
copy
print ?
- #!/bin/bash
- echo "Please enter a user:"
- read a
- b=$(whoami) #使用一个定义过的变量,只要在变量名前面加美元符号($)即可
- if test $a = $b #test 字符串1=字符串2 字符串是否相等,若相等返回true
- then echo "the user is running."
- else echo "the user is not running."
- fi
#!/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi
6.删除当前目录下大小为0的文件
[plain] view plain
copy
print ?
- #!/bin/bash
- for filename in `ls`
- do
- if test -d $filename #test –d File 文件并且是目录
- then b=0
- else
- a=$(ls -l $filename | awk '{ print $5 }')
- if test $a -eq 0 #test 整数1 –eq 整数2 整数相等
- then rm $filename
- fi
- fi
- done
#!/bin/bash
for filename in `ls`
do
if test -d $filename
then b=0
else
a=$(ls -l $filename | awk '{ print $5 }')
if test $a -eq 0
then rm $filename
fi
fi
done
7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G
[plain] view plain
copy
print ?
- #!/bin/bash
- while line=`ls /export/um_lpp_source` //把目录/export/um_lpp_source下的文件赋给line变量
- do
- if test $line="" #if test (表达式为真)
- then echo "NULL" #输出字符串NULL
- sleep 1 #sleep 1 睡眠1秒
- else echo $line
- chfs -a size=3G /export/um_lpp_source #把/export/um_lpp_source的大小改为3G
- exit 0
- fi
- done
#!/bin/bash
while line=`ls /export/um_lpp_source`
do
if test $line=""
then echo "NULL"
sleep 1
else echo $line
chfs -a size=3G /export/um_lpp_source
exit 0
fi
done
8.测试IP地址
[plain] view plain
copy
print ?
- #!/bin/bash
- for i in 1 2 3 4 5 6 7 8 9
- do
- echo "the number of $i computer is "
- ping -c 1 192.168.0.$i #-c指定要求完成的回应次数
- done
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo "the number of $i computer is "
ping -c 1 192.168.0.$i
done
9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件拷贝到当前目录下
[plain] view plain
copy
print ?
- #!/bin/sh
- a=2
- while name="test.log"
- do
- sleep 1 #sleep 1 睡眠1秒
- b=$(ls -l $name | awk '{print $5}')
- if test $b -gt $a #test 整数1 –gt 整数2 整数1大于整数2
- then `cp /opt/*.tar.gz .`
- exit 0
- fi
- done
#!/bin/sh
a=2
while name="test.log"
do
sleep 1
b=$(ls -l $name | awk '{print $5}')
if test $b -gt $a
then `cp /opt/*.tar.gz .`
exit 0
fi
done
10.打印读取的内容,为下面的例子做准备
[plain] view plain
copy
print ?
- #!/bin/bash
- while read name
- do
- echo $name
- done
#!/bin/bash
while read name
do
echo $name
done
11.从0.sh中读取内容并打印
[plain] view plain
copy
print ?
- #!/bin/bash
- while read line
- do
- echo $line
- done < 0.sh
#!/bin/bash
while read line
do
echo $line
done < 0.sh
12.读取a.c中的内容并做加1运算
[plain] view plain
copy
print ?
- #!/bin/bash
- test -e a.c #test –e File 文件是否存在 (常用)
- while read line
- do
- a=$(($line+1)) #内容做加1运算
- done < a.c
- echo $a
#!/bin/bash
test -e a.c
while read line
do
a=$(($line+1))
done < a.c
echo $a
13.普通无参数函数
[plain] view plain
copy
print ?
- #!/bin/bash
- p ()
- {
- echo "hello"
- }
- p
#!/bin/bash
p ()
{
echo "hello"
}
p
14.给函数传递参数
[plain] view plain
copy
print ?
- #!/bin/bash
- p_num ()
- {
- num=$1
- echo $num
- }
- for n in $@ #输入参数到函数中
- do
- p_num $n
- done
#!/bin/bash
p_num ()
{
num=$1
echo $num
}
for n in $@
do
p_num $n
done
15.创建文件夹
[plain] view plain
copy
print ?
- #!/bin/bash
- while :
- do
- echo "please input file's name:"
- read a
- if test -e /root/$a #test –e File 文件是否存在 (常用)
- then
- echo "the file is existing Please input new file name:"
- else
- mkdir $a
- echo "you aye sussesful!"
- break
- fi
- done
#!/bin/bash
while :
do
echo "please input file's name:"
read a
if test -e /root/$a
then
echo "the file is existing Please input new file name:"
else
mkdir $a
echo "you aye sussesful!"
break
fi
done
16.获取本机IP地址
[plain] view plain
copy
print ?
- #!/bin/bash
- ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
#!/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
17.查找最大文件
[plain] view plain
copy
print ?
- #!/bin/bash
- a=0
- for name in *.*
- do
- b=$(ls -l $name | awk '{print $5}')
- if test $b -gt $a #test 整数1 –gt 整数2 整数1大于整数2
- then a=$b #更改比较值
- namemax=$name
- fi
- done
- echo "the max file is $namemax"
#!/bin/bash
a=0
for name in *.*
do
b=$(ls -l $name | awk '{print $5}')
if test $b -gt $a
then a=$b
namemax=$name
fi
done
echo "the max file is $namemax"
18.查找当前网段内IP用户,重定向到ip.txt文件中
[plain] view plain
copy
print ?
- #!/bin/bash
- a=1
- while :
- do
- a=$(($a+1)) #从1开始不断地向上加,直到等于255时退出
- if test $a -gt 255 #test 整数1 –gt 整数2 整数1大于整数2
- then break
- else
- echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') #-c 后面带发送次数 ,sed 's/://g'是吧空格全部替换为\
- ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
- echo $ip >> ip.txt
- fi
- done
TTL,存活时间
TTL是IP协议包中的一个值,它告诉网络路由器包在网络中的时间是否太长而应被丢
#!/bin/bash
a=1
while :
do
a=$(($a+1))
if test $a -gt 255
then break
else
echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
echo $ip >> ip.txt
fi
done
19.打印当前用户
[plain] view plain
copy
print ?
- #!/bin/bash
- echo "Current User is :"
- echo $(ps | grep "$$" | awk '{print $2}') #ps显示瞬间行程 (process) 的动态,$$是当前进程号,就是你的shell的进程号
#!/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')
20.case语句练习
[plain] view plain
copy
print ?
- #!/bin/bash
- clear
- echo "enter a number from 1 to 5:"
- read num
- case $num in
- 1) echo "you enter 1"
- ;;
- 2) echo "you enter 2"
- ;;
- 3) echo "you enter 3"
- ;;
- 4) echo "you enter 4"
- ;;
- 5) echo "you enter 5"
- ;;
- *) echo "error" #如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令
- ;;
- esac
#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
1) echo "you enter 1"
;;
2) echo "you enter 2"
;;
3) echo "you enter 3"
;;
4) echo "you enter 4"
;;
5) echo "you enter 5"
;;
*) echo "error"
;;
esac
21.yes/no返回不同的结构
[plain] view plain
copy
print ?
- #!/bin/bash
- clear
- echo "enter [y/n]:"
- read a
- case $a in #调用变量a
- y|Y|Yes|YES) echo "you enter $a"
- ;; #;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
- n|N|NO|no) echo "you enter $a"
- ;;
- *) echo "error"
- ;;
- esac
#!/bin/bash
clear
echo "enter [y/n]:"
read a
case $a in
y|Y|Yes|YES) echo "you enter $a"
;;
n|N|NO|no) echo "you enter $a"
;;
*) echo "error"
;;
esac
22.内置命令的使用
[plain] view plain
copy
print ?
- #!/bin/bash
- clear #清除终端屏幕
- echo "Hello, $USER" #$USER是环境变量
- echo "Today 's date id `date`" #显示命令date
- echo "the user is :"
- who #who命令能够打印 当前都有谁登录到系统中 的相关信息
- echo "this is `uname -s`" #uname -s`打印当前系统相关信息,-s或--sysname:显示操作系统名称
- echo "that's all folks! "
#!/bin/bash
clear
echo "Hello, $USER"
echo "Today 's date id `date`"
echo "the user is :"
who
echo "this is `uname -s`"
echo "that's all folks! "
23.打印无密码用户
[plain] view plain
copy
print ?
- #!/bin/bash
- echo "No Password User are :"
- echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}') #BEGIN{ 这里面放的是执行前的语句 },从也就是第1个参数