Shell编程练习题

时间:2023-02-12 22:24:47

一、编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息。

# !/bin/bash
if ! grep "^#!" $1&>/dev/null ;
then
cat >> $1<< EOF
#!/bin/bash
# Author:
#Date & Time: `date +"%F %T"`
#Description:
EOF
fi
vim +5 $1




 

二、任意三个整数,判断最大数。

#!/bin/bash
echo "pleaseenter three number:"
read-p "the first number is :" n1
read-p "the second number is:" n2
read-p "the third number is:" n3
let MAX=$n1
if [ $n2 -ge $n1]; then
MAX=$n2
fi
if [ $n3 -ge $MAX]; then
MAX=$n3
fi
echo "the maxnumber is $MAX."


 

三、求100以内偶数的和。

#!/bin/bash
## Author:
#Date & Time:2015-08-29 10:36:13
#Description:

echo "computethe sum of the even number which between 1 and 100......"

let sum=0

for i in $(seq 1100)
do
if [ $[$i%2] == 0 ]
then
let sum+=$i
fi
done
echo "The sumof the even number is $sum ......"


 

四、利用for语句ping B类网。

#!/bin/bash
## Author:
#Date & Time:2015-08-29 11:14:16
#Description:
echo "ping Bnetwork"
read -p "BNetwork :" Bnet
PingNet=`echo$Bnet | sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\)\..*/\1/g'`
for i in {0..255}
do
for j in {1..255}
do
if ping -c1 -W2 $PingNet.$i.$j&>/dev/null
then
echo"$PingNet.$i.$j is online"
else
echo"$PingNet.$i.$j is offline"
fi
done
done


五、提示输入一个用户名,判断用户是否存在,如果存在,显示一下用户默认的shell

#!/bin/bash
## Author:
#Date & Time:2015-08-29 20:03:24
#Description:

echo "pleaseinput the name of user which will be judged..."

read -p "theinput username is :" USER

if cut -d: -f1/etc/passwd | grep "^$USER" &> /dev/null
then
MyBash=`grep "$USER"/etc/passwd | cut -d: -f7`
echo "$USER's bash is :$MyBash"
else
echo "$USER dose notexist"
exit 4
fi


 

六、监控系统登录人数,超过四个,显示已经达到四个,5S检查一下,并退出脚本(exit)

#!/bin/bash
## Author:
#Date & Time:2015-08-29 20:21:57
#Description:


echo "pleaseinput the name of user..."
read -p "Theusername is :" USER

cut -d: -f1/etc/passwd | grep "^$USER" &> /dev/null || exit 6
let COUNT=`who |grep "$USER" | wc -l`
until [ $COUNT -ge4 ]
do
sleep 5
let COUNT=`who | grep"$USER" | wc -l`
done
echo "$USERloged 4 times."


 

七、用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到/dev目录下。

#!/bin/bash
## Author:
#Date & Time:2015-08-29 21:13:22
#Description:

echo "Theprogram will judge that if a file that input is a device file or blockfile."

read -p "Thename of file is :" FileName

if [ -b"$FileName" -o -c "$FileName" ]
then
echo "$FileName is a device file"&& cp $FileName /dev/&
else
echo "$FileName is not a devicefile" && exit 1
fi


 

八、自动关机脚本

每隔一定时间检测一次,如果不存在某个进程就关机,如果存在就休眠。
用法: ./脚本名 进程名 休眠时间
#!/bin/bash
## Author:
#Date & Time:2015-08-31 15:14:42
#Description:

while :
do
threadID=`ps -e | grep "$1"| wc -l`
if [ $threadID -eq 0 ]; then
date >>shutdownTest.log
shutdown -h now
exit
else
echo "Sleeping $2second..."<pre name="code" class="plain">

 

九、当输入某个指定参数时,系统将在一分钟后自动关机:

形式:    ./脚本  参数

</pre><pre name="code" class="plain">#!/bin/bash
## Author:
#Date & Time:2015-08-31 15:42:37
#Description:

orderParam=$1
echo "Theinput param is :" $orderParam

time=`date -d '+1min' "+%H:%M"`
if [ $orderParam ="muyanmoyang" ]
then
echo 'I am sorry about that!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
shutdown -h $time
echo 'Bye Bye!'
else
echo "the input character :$orderParam does not have any effection on the system..."
fi


sleep $2
fi
done

十、对文件进行重命名操作:输入参数包括

1.  文件旧名称

2.  文件新名称

3. 文件目录

#!/bin/bash
## Author:
#Date & Time: 2015-09-02 18:47:02
#Description:


while true
do
read -p "the old file is :" oldFile
if [ $oldFile = "quit" ]
then
exit 0
else
read -p "the new name of the file is :" newFile
read -p "the Directory is:" dir
echo "the directory is :" $dir
cd $dir
mv "$oldFile" "$newFile"
echo "the $oldFile has been renamed to $newFile......"
fi
done


十一、for循环

for(( 初始值; 限制值; 步长))

do

程序

done

#!/bin/bash
## Author:
#Date & Time: 2015-09-03 17:35:02
#Description:

read -p "please input a number :" NUM
sum=0

for (( i=1; i<=$NUM; i=i+1 ))
do
sum=$(($sum+$i))
done
echo "the sum is : $sum"

十二、获取本机的IP地址和网关地址

#!/bin/bash
## Author:
#Date & Time: 2015-09-05 10:17:46
#Description:

file="/etc/sysconfig/network-scripts/ifcfg-Auto_eth3"
if [ -f $file ]
then
IP=`grep "IPADDR" $file| awk -F"=" '{print $2}'`
gateway=`grep "GATEWAY" $file| awk -F"=" '{print $2}'`
echo "IpAddr: $IP Gateway: $gateway"
exit 1
fi


十三、将/etc目录下的所有文件压缩至目录:/root/bak/

#!/bin/bash
## Author:
#Date & Time: 2015-09-06 09:58:39
#Description: the shell is to compress the file in the directory of /etc/ in the specified time

fileDir=`ls /root | grep bak`

if [ -z $fileDir ]
then
mkdir /root/bak/
cd /root/bak/
fi

YY=`date +%y`
MM=`date +%m`
DD=`date +%d`

BACKETC=$YY$MM$DD_etc.tar.gz
tar -zcvf $BACKETC /etc

echo "the files in /etc/ have been compressed"

十四、在/userdata目录下建立50个目录(user1~user50),并赋予目录754权限:

#!/bin/bash
## Author:
#Date & Time: 2015-09-06 10:37:53
#Description:


i=1
while [ $i -le 50 ]
do
if [ -d /userdata ]
then
mkdir -p -m 754 /userdata/user$i
echo "user$i"
i=$(($i+1))
else
mkdir /userdata/
mkdir -p -m 754 /userdata/user$i
echo "user$i"
i=$(($i+1))
fi
done