本章内容:
构建基本脚本
使用多个命令
创建shell脚本文件
构建基本脚本
在构建shell脚本文件时,必须在文件的第一行指定要使用的shell。其格式为:
#!/bin/bash
模拟linux登陆shell
#!/bin/bash
#
echo -n "Login:"
read name
echo -n "Password:"
read -s password
if [ $name='abc' -a $password='abc' ];then
echo -n "Login in Success!"
else
echo -n "Input Error"
fi
比较两个数大小
#!/bin/bash
#
echo "please enter two number"
read x
read y
if test $x -eq $y
then
echo "No.1 == No.2"
elif test $x -gt $y
then
echo "No.1 > No.2"
else
echo "No.1 < No.2"
fi
查找/var/目录下是否存在该文件
#!/bin/bash
#
echo "Enter a file name you want to search:"
read f
if test -e /var/$f
then
echo "the file is exist!"
else
echo "Sorry the file is not exist!"
fi
for循环的使用
#!/bin/bash
#
for i in `seq 1 10`
do
echo $i
done
命令行输入
#!/bin/bash
echo "Please enter a user"
read x
y=$(whoami)
if test $x = $y
then
echo "The user is running"
else
echo "The user is not running"
fi
删除当前目录下大小为0的文件
#!/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 -f $filename
fi
fi
done
如果/export/um_lpp_source下有文件,那么将其文件系统大小改为4G
#!/bin/bash
#
while line=`ls /export/um_lpp_source`
do
if test $line=""
then
echo "NULL"
sleep 1
else
echo "$line"
chfs -a size=4G /export/um_lpp_source
exit 0
fi
done
测试IP地址是否ping得通
#!/bin/bash
for i in `seq 1 10`
do
echo "the number of $i computer is:"
ping -c 1 192.168.2.$i
done
查看test.log的大小大于0,那么将/opt/目录下的*.tar.gz文件复制到当前目录
#!/bin/bash
#
a=2
while name="test.log"
do
sleep 1
b=$(ls -l $name| awk '{print$5}')
if test $b -ge $a
then
cp /opt/*.tar.gz .
exit 0
fi
done
从0.sh中读取内容并打印
#!/bin/bash
#
while read line
do
echo $line
done < 0.sh
#利用管道符可以更加直观
cat 0.sh | while read line
do
echo "$line"
done
读取a.c中的内容并做加1运算
#!/bin/bash
#
test -e a.c
while read line
do
a=$(($line+1))
done < a.c
echo $a
普通无参数的函数
#!/bin/bash
#
p()
{
echo "hello world"
}
p
给函数传递函数
#!/bin/bash
#
p_num()
{
num=$1
echo $num
}
for n in $@
do
p_num $n
done
创建新的文件夹
#!/bin/bash
#
while true:
do
echo "please input file's name:"
read a
if test -e /root/$a
then
echo "the file is exist please input a new file name"
else
mkdir -p $a
echo "create directory successfully!"
break
fi
done
获取本机ip地址
#!/bin/bash
#
ifconfig | grep "inet addr." | awk '{print $2}' | sed 's/addr://g'
#172.16.110.161
#127.0.0.1
#192.168.122.1
查找某个目录下最大的文件
#!/bin/bash
#
a=0
for name in "."
do
b=$(ls -l $name | awk '{print $5}')
if test $b -ge $a
then
a=$b
namemax=$name
fi
done
echo "the max file is $namemax"
查找当前网段内IP用户,重定向到ip.txt文件中
#!/bin/bash
#
a=1
while:
do
a=$(($a+1))
if test $a -gt 255
then
break
else
echo $(ping -c 192.168.0.$a | grep "ttl" | awk '{print $4}' | sed 's/://g')
ip=$(ping -c 192.168.0.$a | grep "ttl" | awk '{print $4}' | sed 's/://g')
echo $ip > ip.txt
fi
done
打印当前用户
#!/bin/bash
#
echo "Current login user is :"
echo $(whoami)
echo "Current TTY is:"
echo $(ps | grep "$$" | awk '{print$2}')
case语句练习
#/bin/bash
#
clear
echo "enter a number from 1-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 "out of range,errror"
;;
esac
yes/no 返回不同的结构
#!/bin/bash
#
clear
echo "Enter [y|n]:"
read a
case $a in
yes|y|Y|YES) echo "You enter $a"
;;
no|NO|N|n) echo "You enter $a"
;;
*) echo "Error input "
;;
esac
杀进程
#!/bin/bash
#
pid=$(ps -ef | grep "进程相关内容" | grep -v grep | awk '{print$2}')
if [ -n "$pid" ]
then
kill -9 $pid
fi
内置命令的使用
#!/bin/bash
#
clear
#利用环境变量
echo "Hello $USER"
echo "Today's date is `date +%F`"
echo "the user is : $(whoami)"
echo "the os is `uname -s`"
打印无密码用户
#!/bin/bash
#
echo "No Pssword Users are:"
#这边!!需要用转义符进行转义
echo $(cat /etc/shadow | grep "\!\!" | awk 'BEGIN {FS=":"}{print $1}')
检查端口号是否已启动
#!/bin/bash
#
n=1
echo "检查xxx服务..."
while true:
do
if test $n -gt 20
then
echo "xxx服务启动失败"
break
fi
sleep 5
n=$(($n+1))
port=`netstat -antp| grep "0.0.0.0:8080"`
if [ ${#port} -gt 3 ]
then
echo "xxx服务已经启动"
break
fi
done