shell脚本编程基础面试题

一级标题

shell脚本编程基础

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核
版本, CPU型号,内存大小,硬盘大小


[root@magedu ~]# vim sysinfo.sh
#!/bin/bash
ipv4=`ifconfig |egrep -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|
[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>){2}\.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-
9]|25[0-5])\>"`
echo "当前主机系统信息如下:"
echo 主机名: `hostname`
echo ipv4地址:"$ipv4"
echo 操作系统版本: `cat /etc/redhat-release |cut -d. -f1-2`
echo 内核版本: `uname -r`
echo CPU型号: `lscpu |grep "Model name"|tail -1 |tr -s ' '|cut -d: -f2`
echo 内存大小: `free -mh |head -2|tail -1|tr -s ' '|cut -d' ' -f2`
echo 硬盘容量: `fdisk -l|head -2|tail -1|cut -d, -f1|cut -d' ' -f2-4`
带颜⾊的版本。

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
[root@magedu ~]# vim /root/bin/backup.sh
#!/bin/bash
cp -a /etc/ /root/etc`date +%Y-%m-%d`
echo "/etc目录备份完成"


3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@magedu ~]# vim /root/bin/disk.sh
#!/bin/bash
echo "当前硬盘分区中空间利用率最大的值: `df |grep /dev/sd |grep -o "[0-9]\{1,3\}%" |sort -rn
|head -1`


4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小
排序

[root@magedu ~]# vim /root/bin/links.sh
#!/bin/sh
echo "每个远程主机的IPv4地址和连接数:`
netstat -tan |tr -s " " ":"|cut -d: -f6|grep ^[[:digit:]]|sort|uniq -c |sort -nr`"
1、编写脚本sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
[root@magedu ~]# vim /root/bin/sumid.sh
#!/bin/bash
ip1=`cat /etc/passwd |head -10 | tail -1|cut -d: -f3`
ip2=`cat /etc/passwd |head -20 | tail -1|cut -d: -f3`
let sum=ip1+ip2
echo $sum
2、编写脚本sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和##

[root@magedu ~]# vim /root/bin/sumspace.sh
#!/bin/sh
space1=`cat $1 |grep "^$"|wc -l`
space2=`cat $2 |grep "^$"|wc -l`
let sum=space1+space2
echo "两个文件中所有空白行之和:$sum"
3、编写脚本sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

[root@magedu ~]# vim /root/bin/sumfile.sh
#!/bin/bash
numetc=ls -A /etc|wc -l
numvar=ls -A /var|wc -l
numusr=ls -A /usr|wc -l
let sum=numetc+numvar+numusr
echo $sum
1、编写脚本argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参
数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[root@magedu ~]# vim argsnum.sh
#!/bin/bash
[[ $# -lt 1 ]] && (echo "at least one argument"&&exit)|| echo `grep '^$' $1|wc -l`
2、编写脚本hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户
“该IP地址可访问” ;如果不可ping通,则提示用户“该IP地址不可访问”
[root@magedu ~]# vim hostping.sh
#!/bin/bash
ping -c1 -w1 $1 & > /dev/null && echo "$1 is up"||echo "$1 is down"
3、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将
满

[root@magedu ~]# vim /root/bin/checkdisk.sh
#!/bin/bash
disk=`df|egrep /dev/sd|tr -s ' ' '%'|cut -d% -f5|sort -nr|head -n1`
inode=`df -i|egrep /dev/sd|tr -s ' ' '%'|cut -d% -f5|sort -nr|head -n1`
[ $disk -ge 80 -o $inode -ge 80 ] && wall space will full.
4、编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
[root@magedu ~]# vim per.sh
#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "$1 is not read and write" || exit
注意:执行时请以一般用户执行,以root执行无法显示结果。



5、编写脚本excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用
户非脚本文件
[root@magedu ~]#vim excute.sh
#!/bin/bash
[[ $1 =~ .sh$ ]] && [ -f $1 ] && (chmod a+x $1;echo "$1 is .sh")||echo "$1 is not .sh"


6、编写脚本nologin.sh和login.sh,实现禁止和允许普通用户登录系统

[root@magedu ~]# vim nologin.sh
#!/bin/bash
[ -f /etc/nologin ] && echo “nologin”|| (touch /etc/nologin;echo “nologin”)
[root@magedu ~]# vim login.sh
#!/bin/bash
[ -f /etc/nologin ] && ( rm -f /etc/nologin;echo “login”)||echo “login”

1、编写脚本createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存
在,否则添加之;显示添加的用户的id号等信息
[root@magedu ~]# vim createuser.sh
#!/bin/bash
read -p "please input username:" user
id $user &>/dev/null
[[ ! $? -eq 0 ]] && (useradd $user &> /dev/null && echo "add $user user") ||echo "the user
is exits"

2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
[root@magedu ~]# vim /root/bin/yesorno.sh
#!/bin/bash
read -p "Do you agree? (Yes or No): " ANS
[[ "$ANS" =~ ^[Yy]([Ee][Ss])?$ ]] && { echo OK ; exit; }
[[ "$ANS" =~ ^[Nn][Oo]?$ ]] && { echo Not OK ; exit;}
echo "Your input is false"


3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件
类型)
[root@magedu ~]# vim /root/bin/filetype.sh
#!/bin/bash
read -p "please input file path:" path
a=`ls -l $path|grep -o "^."`
case $a in
-)
echo "common file" ;;
d)
echo "directory file" ;;
l)
echo "linked file" ;;
*)
echo "other file" ;;
esac
if [ -z "$1" ]; then
echo there must be a filename argument
exit 1
elif [ ! -e "$1" ]; then
echo no such file
exit 1
elif [ -d "$1" ]; then
echo directory file
exit 0
elif [ -L "$1" ]; then
echo link file
exit 0
elif [ -f "$1" ]; then
echo normal file
exit 0
else
echo other file
exit 0
fi


4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
[root@magedu ~]# vim /root/bin/checkint.sh
#!/bin/bash
read -p "please input a number:" num
[[ $num =~ ^[0-9]+$ ]] && echo “$num is int”||{echo ”lease input a number”;exit;}
1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
[root@magedu ~]# vim /etc/profile
export PATH=/usr/local/apache/bin:$PATH
[root@magedu ~]# source /etc/profile 生效

2、用户root登录时,将命令指示符变成红色,并自动启用如下别名:
rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或
ifcfg-ens33 ’ (如果系统是CentOS7)
[root@magedu ~]# vim ~/.bashrc
#!/bin/bash
alias rm='rm –i'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias editnet1='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
alias editnet2='vim /etc/sysconfig/network-scripts/ifcfg-ens33'
export PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]' 红色
[root@magedu ~]# source ~.bashrc 生效

3、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
[root@magedu ~]# vim /etc/issue
^[[031m " Hi,dangerous!" ^[[0m"


4、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@magedu ~]# vim .vimrc
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#")
call
setline(3,"#********************************************************************")
call setline(4,"#Author: w")
call setline(5,"#QQ: 1****")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: http://www.magedu.com")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call
setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G


5、编写用户的环境初始化脚本reset.sh,包括别名,登录提示符, vim的设置,环境变量等
[root@magedu ~]# vim reset.sh
#!/bin/bash
cat > /etc/profile.d/env.sh << EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias editnet="vim /etc/sysconfig/network-scripts/ifcfg-ens33"
export PS1="[\[\e[32m\]\u\[\e[36m\]@\h_v7\[\e[31m\]\[\e[0m\] \W]\\$ "
export PATH=/app/bin:$PATH
EOF
cat > ~/.vimrc << EOF
set nu
EOF

猜你喜欢

转载自www.cnblogs.com/zhaihongyu/p/12686000.html
今日推荐