shell 习题

shell 习题

[@more@]

1.对两个数分别求和、差、积、商、余,两个数由脚本参数传递
[root@ shell]# cat 1.sh
#!/bin/bash

#var2=$1
#var2=$2
read -p "Enter 1st number: " var1
read -p "Enter 2se number: " var2
#if expr "$1" + 0 &>/dev/null
if grep -qE '^[0-9]+$' <<< $var1 &>/dev/null
then
#if expr "$2" + 0 &>/dev/null
if grep -qE '^[0-9]+$' <<< $var2 &>/dev/null
then
echo $var1 + $var2 = $[var1+var2]
echo $var1 - $var2 = $((var1-var2))
echo $var1 x $var2 = `expr $var1 * $var2`
echo $var1 / $var2 = $(echo $var1 / $var2 | bc)
let var=$var1%$var2
echo $var1 % $var2 = $var
else
echo "2se number error"
exit 1
fi
else
echo "1st number error"
exit 1
fi
[root@ shell]#

2.使用for循环批量添加10个用户,用户名分别是user1~user10,密码为uplooking。
[root@ shell]# cat 2.sh
#!/bin/bash
for i in {1..10}
do
#! id user$i
#&& useradd user$i
# && echo uplooking | passwd --stdin user$i
#userdel -r user$i
if useradd user$i &>/dev/null
then
passwd user$i << EOF
uplooking
uplooking
EOF
fi
done &>/dev/null
[root@ shell]#
3.执行脚本,判断某个目录的大小,并以邮件方式发给root用户
[root@ shell]# cat 3.sh
#!/bin/bash

if [ -d "$1" ]
then
mail root <<< $(du -sh $1)
else
echo "$1 non directory"
exit 1
fi
[root@ shell]#
4,提取出/usr/share/doc目录下的所有的文件名为index.html的文件。把他们集中放在/tmp/index目录中。文件名字按提取顺序更名标记。即:第一个找到的index.html命名为index.html.1。第二个为index.html.2。以此类推。
#!/bin/bash

mkdir -p /tmp/index

for i in $(find /usr/share/doc/ -type f -name "index.html")
do
cp $i /tmp/index/index.html.$((++n))
done

5,写一个脚本报告用户信息,这个脚本只能添加一个参数,此参数为用户名。脚本可以报告系统中是否有此用户。如果有,程序报告:User exist。如果不存在,报告:No this user。

6,写一个脚本,统计出你系统/dev 目录下所有设备文件已以及管道文件,套接字文件的总数
[root@ shell]# cat 5.sh
#!/bin/bash

#for i in b c p s
#do
# echo "$i count: "`find /dev -type $i | wc -l`
#done

for i in $(find /dev)
do
if [ -b $i ]
then
((B++))
elif [ -c $i ]
then
((C++))
elif [ -p $i ]
then
((P++))
elif [ -S $i ]
then
((S++))
fi
done

echo -e "block :$Bnchar :$Cnpipe :$PnSCOKET :$S"

[root@ shell]#

7,Write a shell script only to list the hidden items of a designated directory. The designated directory must be acquired as a command parameter. If the parameter is not a directory, print a warning message.
[root@ shell]# cat 7.sh
#!/bin/bash

if [ -d "$1" ]
then
ls -A $1 | grep '^.'
else
echo "$1 non directory"
exit 1
fi
[root@ shell]#

写了一个shell脚本,只列出指定目录中的隐藏项目。指定的目录必须由脚本参数传递。如果参数是不是一个目录,打印一个警告消息。

8,Write a shell script to remove all the empty .txt files in your current directory and print the number of removed files.

写一个shell脚本来删除当前目录所有空.txt文件。并且打印删除文件的数量。

[root@ shell]# cat 8.sh
#!/bin/bash

for i in $(find . -maxdepth 1 -type f -name "*.txt")
do
files="$files $i"
[ ! -s $i ] && rm -f $i && ((n++))
done
echo $n
echo del files: $files
[root@ shell]#
9,There is a famous game which is "counting seven". Now we use shell script to implement the game. Print the numbers from 1 to 1000, omitting the number which has 7 in it or is a multiple of 7.

有一个著名的游戏这是“数七“。现在我们使用shell脚本来执行游戏。打印数字从1到1000,省略了数目中有7或者是7的倍数。
[root@ shell]# cat 9.sh
#!/bin/bash

for i in {1..1000}
do
[[ ! $i =~ 7 && $[$i%7] -ne 0 ]] && echo $i
done
[root@ shell]#

10.lock
当脚本正在运行时,不能在执行此脚本,提示:“scripts is running..."
[root@ shell]# cat 10.sh
#!/bin/bash

if [ -e /var/lock/subsys/$(basename $0) ]
then
if [ -e /var/run/$(basename $0).pid ]
then
if [ -e /proc/$(cat /var/run/$(basename $0).pid) ]
then
echo "scripts is running..."
exit 1
fi
fi
fi

if uname -r &> /dev/null
then
touch /var/lock/subsys/$(basename $0)
echo $$ > /var/run/$(basename $0).pid
fi
sleep 3
rm -rf /var/lock/subsys/$(basename $0)
rm -rf /var/run/$(basename $0).pid


[root@ shell]#

11. 将user.list中的用户添加到系统中去 并给用户设置passwd.list文件中对应的密码
user.list 文件内容:

#!/bin/bash
while read username groups
do
for i in $(sed 's/,/ /g' <<< $groups)
do
grep -q ^$i: /etc/group || groupadd $i
done
useradd $username -g `sed 's/,/ -G /' <<< $groups` && awk '$1 == "'$username'"{print $2}' passwd.list| passwd --stdin $username &>/dev/null
done < user.list

12.写个脚本txtdel.sh,当运行后有下列运行结果:
无参数,扫描当前文件夹下所有.txt结尾的文件,列出前三行,提示是否要删除.

$ ls

food.txt
myprog.java
diary.txt
tempfile
story.txt

$ txtdel.sh

File: food.txt
Line 1: Chicken rice
Line 2: Hamburgers
Line 3: Curry
Delete? Y (user input)
File food.txt has been deleted.

File: diary.txt
Line 1: Today I got up
Line 2: went to university
Line 3: and learned about UNIX!
Delete? N (user input)
File diary.txt was NOT deleted.

File: story.txt
Line 1: It was a cold and stormy night…
Line 2: lots of rain…
Line 3: really quite awful…
Delete? Y (user input)
File story.txt has been deleted.

No more .txt files to process.
deleted file : diary.txt
deleted count: 1
Finished!

#!/bin/bash

for i in $(find . -type f -name "*.txt")
do
echo File: $i
awk 'NR<=3{print "Line "NR": "$0}' $i
NUM=1
while [ $NUM -eq 1 ]
do
read -p "Delete? (Y|N)" var
case $var in
Y|y|yes|YES)
rm -f $i
((del_count_file++))
del_files="$del_files $i"
echo "File $i has been deleted."
NUM=2
echo
;;
N|n|NO|no)
echo "File $i was NOT deleted."
NUM=2
echo
;;
*)
echo "only (Y|N)"
esac
done
done

echo "No more .txt files to process. "
echo "deleted file : $del_files"
echo "deleted count: $del_count_file"
echo "Finished!"

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24870090/viewspace-1050397/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24870090/viewspace-1050397/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值