答案就在下一行,鼠标选中就看到了 像这样 像这样
1、编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 2、编写脚本excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件 3、编写脚本nologin.sh和login.sh,实现禁止和允许普通用户登录系统 4、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数 5、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问” 6、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满 Bash的文件测试 7、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和 8、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和 9、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件 10、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息 11、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息 12、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型) 13、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数 14、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 15、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中 16、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值 17、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序 18、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin 19、用户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) 20、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!” 21、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等 22、编写用户的环境初始化脚本reset.sh,包括别名,登录提示符,vim的设置,环境变量等
1、编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
#!/bin/bash if [ -e $1 ] then [ ! -r $1 -a ! -w $1 ] && echo "The file is not write and not red" || echo "The file can read or write" else echo "The file isn't exist" fi2、编写脚本excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
#!/bin/bash [ -f $1 -a $(echo $1 | grep ".*\.sh") ] && chmod 755 $1;echo "yesyes" || echo " isn't a script"3、编写脚本nologin.sh和login.sh,实现禁止和允许普通用户登录系统
#!/bin/bash loginyes=none nologin=none [ -f /etc/nologin ] && read -n 1 -p "Do you want to allow user login[Y/N]:" nologin || read -n 1 -p "Do you want to forbid user login[Y/N]: " loginyes loginyes=$(echo $loginyes | tr 'A-Z' 'a-z') nologin=$(echo $nologin | tr 'A-Z' 'a-z') #echo -e "your choose is $loginyes \n please wait! \n ... \n Done,all of users was forbid to login." #echo "your choose is $nologin \n please wait! \n ... \n Done,all of users was allowed to login." if [ $loginyes = 'y' ] then echo -e "\nyou choose yeyesyess" touch /etc/nologin elif [ $loginyes = 'n' ] then echo -e "\nyou choose NO" fi if [ $nologin = 'y' ] then echo -e "\n you choose yes" rm -f /etc/nologin elif [ $nologin = 'n' ] then echo -e "\nyou choose No" fi4、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash [ $# -lt 1 ] && echo "you must give me unless one agrument"&&exit 1 || echo `cat $1| grep ^$|wc -l`5、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash #ping ip ping -c 1 $1 > /dev/nul [ $? -eq 0 ] && echo "ip can ping" || echo " ip can't ping"6、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满 Bash的文件测试
#!/bin/bash #test6 disk user% USES=$(df | egrep -o "[0-9]{1,3}%" | tr -d %| sort -nr|head -1) USEI=$(df -i | egrep -o "[0-9]{1,3}%" | tr -d %| sort -nr|head -1) [ $USES -ge 80 ] && wall "WARNING! the disk for almost" [ $USEI -ge 80 ] && wall "WARNING! the inode for almost"7、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#!/bin/bash USER10=$(cat /etc/passwd | head -10 | tail -1|cut -d: -f3) USER20=$(cat /etc/passwd | head -20 | tail -1|cut -d: -f3) SUMUSER=$[ $USER10 + $USER20 ] echo "ID sum is $SUMUSER"8、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#!/bin/bash [ $# -lt 1 ] && echo "please input unless one argument" && exit #SUM1=$(cat $1 | grep ^$|wc -l) #SUM2=$(cat $2 | grep ^$|wc -l) #SUM=$[ $SUM1 + $SUM2 ] sum=$( cat $@ | grep "^$" | wc -l ) echo "the space sum is $sum"9、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash ETC=$( ls -dl /etc/* /var/* /usr/*| wc -l) echo "/etc and /var and /usr have $ETC directories or file"10、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
#!/bin/bash #10.createuser [ $# -lt 1 ] && echo "plead input unless one argument" && exit 1 useradd $1 &> /dev/nul USER_EXIT=$? [ $USER_EXIT -eq 9 ] && echo "createuser.sh: user $1 already exists" && exit 9 [ ! $USER_EXIT -eq 0 ] && echo "createuser.sh: ERROR" id $111、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash read -p "Please input [Yes/No]:" YESORNO case $YESORNO in [Yy][Ee][Ss]|[Yy]) echo "you choose yesyes" ;; [Nn][Oo]|[Nn]) echo "you choose no" ;; *) echo "you choose other" esac12、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash [ $# -lt 1 ] && echo "Please input unless one argument!" && exit for count in `seq 1 $#` do CHAR=$(eval ls -dl \$$count | cut -c 1) CO="\e[1;32m" LOR="\e[0m" case $CHAR in -) echo -e "The file type is \e[1;32mnormal file\e[0m" ;; l) echo -e "The file type is $CO link file $LOR" ;; d) echo -e "The file type is $CO directory $LOR" ;; *) echo "sorry,I still don't know the file type " esac done13、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash [ $# -lt 1 ] && echo "Please intput unless one argument" && exit [ $# -gt 1 ] && echo "I just can understand one argument" && exit [[ $1 =~ ^[0-9]*$ ]] && echo "your argument is a positive integer" || echo "your argument isn't a INT"14、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
#!/bin/bash CO="\e[1;31m" LOR="\e[0m" echo -e "The hostname is $CO `hostname` $LOR" echo -e "The IP address is $CO `ifconfig ens33| egrep -o "(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>"` $LOR" echo -e "The kernel release $CO `uname -r` $LOR" echo -e "The System version is $CO `cat /proc/version | grep -o ".*64"` $LOR" echo -e "The CPU model is $CO `lscpu | grep "Model name"|cut -d: -f2 |tr -d " " ` $LOR" echo -e "The memory is $CO `lsmem | grep "Total online memory"|cut -d: -f2|tr -d " "` $LOR" echo -e "The disk is $CO `lsblk | grep "\<sda\>"|tr -s ' '| cut -d ' ' -f4` $LOR"15、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bash cp -a /etc/ /root/etc`date %F`16、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash echo -e "The disk use ratio is `df | grep -o "...%"|tr -d %|sort -nr|head -1`%"17、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bash #links.sh echo "you host links stat is :" who | egrep -o "(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>"|uniq -c|sort -nr18、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
vim /etc/profile.d/env.sh export PATH=/usr/local/apache/bin:$PATH source /etc/profile.d/env.sh19、用户root登录时,将命令指示符变成红色,并自动启用如下别名:
vim /root/.bashrc ... PS1='\[\e[1;31m\][\u@\h \w]\$\[\e[0m\]' rm=‘rm –i’ cdnet=‘cd /etc/sysconfig/network-scripts/’ editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-ens33’ ...20、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
#任意用户登录系统时,会按如下读取配置,在如下哪个文件写都可以 #/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc vim /etc/profile.d/env.sh echo -e '\033[31m hi,dangerous! \033[0m'21、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等 示例
vim ~/.vimrc set nu "show line set ts=4 "TAB 4 chars syntax on "grammar light set cursorline "set mouse=a set ai 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: Jibill Chen") call setline(5,"#QQ: *********") call setline(6,"#Date: ".strftime("%Y-%m-%d")) call setline(7,"#FileName: ".expand("%")) call setline(8,"#URL: http://thson.blog.csdn.net") call setline(9,"#Description: The test script") call setline(10,"#**********************************************************") call setline(11,"") endif endfunc autocmd BufNewFile * normal G22、编写用户的环境初始化脚本reset.sh,包括别名,登录提示符,vim的设置,环境变量等
vim reset.sh ... cat >> ~/.bash_profile << EOF PS1='\[\e[1;8;$[RANDOM%6+41]m\][\u@\h \w]\$\[\e[0m\]' export REGEX_IP='(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' EOF #config bashrc cat >> ~/.bashrc << EOF alias cdnet='cd /etc/sysconfig/network-scripts/' EOF #config vimrc cat >> ~/.vimrc << EOF set nu "show line set ts=4 "TAB 4 chars syntax on "grammar light set cursorline "set mouse=a set ai 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: Jibill Chen") call setline(5,"#QQ: **********") call setline(6,"#Date: ".strftime("%Y-%m-%d")) call setline(7,"#FileName: ".expand("%")) call setline(8,"#URL: http://thson.blog.csdn.net") call setline(9,"#Description: The test script") call setline(10,"#**********************************************************") call setline(11,"") endif endfunc autocmd BufNewFile * normal G EOF ...