SHELL编程实验题.docx
《SHELL编程实验题.docx》由会员分享,可在线阅读,更多相关《SHELL编程实验题.docx(12页珍藏版)》请在冰点文库上搜索。
SHELL编程实验题
1.如何查看bash手册?
manbash
2.登陆流程中涉及哪些文件?
/etc/bashrc /etc/profile /etc/profile.d/ /root/.bash_profile /root/.bashrc
3.路径环境变量是什么?
$PATH
4.vim如果打卡两个窗口?
vim-Ofile1file2
5.使用find查找/etc目录中今天修改过的文件?
find/etc-typef-daystart-mtime0-ls
6.查看root用户有哪些计划任务?
crontab-l-uroot
7.输出进程列表中还有init字符串的行,要求使用ps、grep通过管道完成。
psaux|grep“init"
8.如何将sleep500,运行任务丢在后台。
nohupsleep500&
9.查找/etc目录下权限r--------的文件。
find/etc/-typef-perm400-ls
10.将ls-l/etc的输入写入文件/tmp/out.txt中。
ls-l/etc/>/tmp/out.txt
11.判断/tmp是目录,则输出ok,要求使用&&。
test-d/tmp&&echo“ok"
12.使用正则过滤root字符串开头的行,在/etc/passwd中。
grep"^root"/etc/passwd
13.使用grep过滤bash字符串结尾的行,在/etc/passwd中,要求高亮显示。
grep"bash$"/etc/passwd
14.使用awk计算/etc/passwd文件中uid累计之和。
awk-F":
"'BEGIN{sum=0;}{sum+=$3;}END{printsum}'/etc/passwd
15.使用sed修改文件/etc/hosts,将127.0.0.1替换成127.0.0.2。
sed-i's/127.0.0.1/127.0.0.2/g'/etc/hosts
16.编写一个文件类型判断脚本。
cat filetypechk.sh
#!
/bin/bash
FILENAME="$1"
if[-f$FILENAME];then
echo"isaregularfile"
fi
if[-d$FILENAME];then
echo"isadirectory"
fi
if[-p$FILENAME];then
echo"isanamedpipe"
fi
if[-S$FILENAME];then
echo"isasocket"
fi
if[-b$FILENAME];then
echo"isblockspecial"
fi
if[-c$FILENAME];then
echo"ischaracterspecial"
fi
if[-L$FILENAME];then
echo"isasymboliclink"
fi
17.编写一个网络扫描脚本。
cat Netscan.sh
#!
/bin/bash
foriin{1..254}
do
(
ifarping-c15192.168.100.$i&>/tmp/tmp$i
then
MACADDR=`grep"Unicast"/tmp/tmp$i|head-1`
MACADDR=${MACADDR%%]*}
MACADDR=${MACADDR##*[}
echo"192.168.100.$i "$MACADDR
fi
rm-rf/tmp/tmp$i
)&
done
sleep15
18.编写一个读取配置文件添加用户。
cat useradd.sh
#!
/bin/bash
CONFFILE="$1"
whilereadLINE
do
USERNAME=`echo$LINE|awk'{print$1}'`
PASSWORD=`echo$LINE|awk'{print$2}'`
useradd$USERNAME&>/dev/null|| echo"USER$USERNAMEadderror!
"
echo"$PASSWORD"|passwd--stdin$USERNAME&>/dev/null
done<$CONFFILE
19.编写一个周期备份脚本。
#!
/bin/bash
TIME=`date+%w`
FILELIST=“$@"
tar-zcf/backup/back$TIME.tar.gz$FILELIST
20.编写一个磁盘空间监控脚本。
#!
/bin/bash
THRESHOLD="40"
df-h|grep"^/">/tmp/disk_monitor.tmp
sendmail(){
MSG="$@"
echo$MSG|mail-s"DiskWarning" xiushi@
}
whilereadLINE
do
USE=`echo$LINE|awk'{print$5}'|tr-d'%'`
if[$USE-ge$THRESHOLD]
then
sendmail$LINE
fi
done
21.快速创建文件
./createfile.sh-s10-f/tmp/tempfile
-s文件尺寸,单位MB
-f 创建文件位置
#!
/bin/bash
print_help()
{
echo"./createfile.sh-s10-f/tmp/tempfile"
echo" -s文件尺寸,单位MB"
echo" -f 创建文件位置"
}
if[$#-lt1]
then
print_help
exit3
else
whilegetoptss:
f:
OPTION
do
case$OPTIONin
s)
SIZE=$OPTARG
;;
f)
FILENAME=$OPTARG
;;
?
)
echo "Error:
IllegalOption."
print_help
exit3
;;
esac
done
fi
ddif=/dev/zeroof=$FILENAMEbs=1MBcount=$SIZE
22.自动FTP脚本
./download_ftp.sh
执行程序可以同步服务器上的制定目录到本地。
#!
/bin/bash
SERVERIP="10.13.24.21"
RDIR="/pub"
LDIR="/tmp/"
USER="ftp"
PASS="ftp"
DEBUG="1"
if[$DEBUG-eq1]
then
lftp--debug-u$USER:
$PASS-e"mirror$RDIR$LDIR;quit"ftp:
//$SERVERIP
else
lftp--debug-u$USER:
$PASS-e"mirror$RDIR$LDIR;quit"ftp:
//$SERVERIP
fi
./upload_ftp.sh
执行程序可以上传本地文件到服务器上。
#!
/bin/bash
SERVERIP="10.13.24.21"
RDIR="/pub"
LDIR="/tmp/"
USER="ftp"
PASS="ftp"
DEBUG="1"
if[$DEBUG-eq1]
then
lftp--debug-u$USER:
$PASS-e"mirror-R $LDIR$RDIR;quit"ftp:
//$SERVERIP
else
lftp--debug-u$USER:
$PASS-e"mirror-R$LDIR$RDIR;quit"ftp:
//$SERVERIP
fi
23.自动选择菜单
./tun.sh
部署在用户登录执行的程序,用于跳转其他服务器,支持安全审核。
#!
/bin/bash
LIST=("root_10.13.49.128""root_127.0.0.1" "xiushi_127.0.0.1""xiushi_10.0.0.1")
whileread-p"Pleaseinputip:
"LINE
do
foriin${LIST[$@]}
do
if["${LOGNAME}_${LINE}"="$i"]
then
ssh${LOGNAME}@$LINE
fi
done
echo"Younotpermissions."
echo
done
24.数据中读取配置信息
./getconfig.shmy.conf
读取数据库中存储的配置信息。
#Testconfigure
[client]
option1=123
option2=456
[server]
option3=789
option4=000
#!
/bin/bash
CONF="$1"
TEMP="/tmp/t.conf"
grep-v"#"$CONF|grep-v"^$">$TEMP
whilereadLINE
do
ifecho"$LINE"|grep"^\[">/dev/null
then
TYPE=$LINE
fi
N=`echo$LINE|awk-F"="'{print$1}'`
V=`echo$LINE|awk-F"="'{print$2}'`
case$TYPEin
"[client]")
case$Nin
"option1")
echo"OPTION1=$V"
;;
"option2")
echo"OPTION2=$V"
;;
*)
if["$TYPE"!
="$LINE"]
then
echo"Configureiserror."
exit1
fi
;;
esac
;;
"[server]")
case$Nin
"option3")
echo"OPTION3=$V"
;;
"option4")
echo"OPTION4=$V"
;;
*)
if["$TYPE"!
="$LINE"]
then
echo"Configureiserror."
exit1
fi
;;
esac
;;
*)
echo"Configureiserror."
exit1
;;
esac
done<$TEMP
25.Mysql服务控制
./mysql_daemon.sh[start|stop|restart]
启动停止重启mysql服务进程。
#!
/bin/bash
start(){
mkdir-p/var/run/mysql
chmod777/var/run/mysql
/usr/bin/mysqld_safe--pid-file="/var/run/mysql/mysql.pid"--user=mysql>/dev/null2>&1&
}
stop(){
PID=`cat/var/run/mysql/mysql.pid`
kill-15$PID
}
TYPE="$1"
case$TYPEin
"start")
start
exit0
;;
"stop")
stop
exit0
;;
"restart")
stop
sleep1
start
;;
*)
echo "./mysql_daemon.sh[start|stop|restart]"
;;
esac
26.收集并备份日志脚本
./backup_logger.sh
收集备份指定目录下日志文件,打包并压缩。
#!
/bin/bash
BACKUPSAVE="/tmp"
BACKUPDIR="/opt"
DATE=`date+%F`
tarzcf$BACKUPSAVE/mybackup_$DATE.tar.gz$BACKUPDIR