本帖最后由 denjon 于 2014-12-23 13:55 编辑
1.编写shell脚本,判断用户输入的字符类型,是字母就输出alpha,是数字就输入digit。
2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。
3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。
4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。
5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。
6.写一个脚本,检测用户输入字符串的合法性,要求字符串由大小写字母、数字组成,无标点、特殊符号和空格。
7.输入一个 “ month day year”格式的日期串,程序将处理“month”为其英文单词的前3个字母,第一个字母大写,其余的小写。
8.检测输入整数的合法性,可为负数(-1、-2均合法),可以指定传入整数的范围,不在范围内则报错。
9.检测浮点数的合法性
要求:参数为一个浮点数,合法性要求不支持科学表示法,支持带符号,如-1.2,-75均合法。
应用:
1.操作系统信息包括计算机名、Linux发行版名称、Linux内核版本和当前的IP地址,现有一台安装了linux分发版的计算机,要求用户写一个shell,在屏幕上输出操作系统信息。
2.实际应用中,用户经常需要通过各种方式对指定目录或文件进行备份。假设当前的计算机用户根目录下存在目录Documents,要求用户编写一个shell脚本对该目录进行备份,压缩为Linux系统中常用的tar.gz格式。
3.现有目录A和目录B,目录中不包含子目录,要求用户编写shell脚本比较两个目录内文件的差异。
4.编写一个shell程序,对df 和ps命令的执行结果进行检查,进而对系统资源使用情况进行判断。检查过程如下:
(1)使用df命令检查磁盘空间使用情况,即查看df命令执行结果的第5列“已用%”是否超过“50%”,若超过50%则将相关信息记录到df_report文档中。
(2) 使用ps命令检查系统各进程所占用的CPU和内存情况,即查看ps命令执行经过的第3列(%cpu)和第4列(%MEM),若其中有一项不等于0.0,则将相关信息记录到文件ps_report中。
5.shell编程批量用户的创建
1)通过命令连续创建50个用户帐号
2)通过操作文件创建多个用户
1.编写shell脚本,判断用户输入的字符类型,是字母就输出alpha,是数字就输入digit。
2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。
3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。
4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。
5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。
6.写一个脚本,检测用户输入字符串的合法性,要求字符串由大小写字母、数字组成,无标点、特殊符号和空格。
7.输入一个 “ month day year”格式的日期串,程序将处理“month”为其英文单词的前3个字母,第一个字母大写,其余的小写。
8.检测输入整数的合法性,可为负数(-1、-2均合法),可以指定传入整数的范围,不在范围内则报错。
9.检测浮点数的合法性
要求:参数为一个浮点数,合法性要求不支持科学表示法,支持带符号,如-1.2,-75均合法。
应用:
1.操作系统信息包括计算机名、Linux发行版名称、Linux内核版本和当前的IP地址,现有一台安装了linux分发版的计算机,要求用户写一个shell,在屏幕上输出操作系统信息。
2.实际应用中,用户经常需要通过各种方式对指定目录或文件进行备份。假设当前的计算机用户根目录下存在目录Documents,要求用户编写一个shell脚本对该目录进行备份,压缩为Linux系统中常用的tar.gz格式。
3.现有目录A和目录B,目录中不包含子目录,要求用户编写shell脚本比较两个目录内文件的差异。
4.编写一个shell程序,对df 和ps命令的执行结果进行检查,进而对系统资源使用情况进行判断。检查过程如下:
(1)使用df命令检查磁盘空间使用情况,即查看df命令执行结果的第5列“已用%”是否超过“50%”,若超过50%则将相关信息记录到df_report文档中。
(2) 使用ps命令检查系统各进程所占用的CPU和内存情况,即查看ps命令执行经过的第3列(%cpu)和第4列(%MEM),若其中有一项不等于0.0,则将相关信息记录到文件ps_report中。
5.shell编程批量用户的创建
1)通过命令连续创建50个用户帐号
2)通过操作文件创建多个用户
0
[password]123[/password]1
#!/bin/bash
read -p "please input zifu leixing:" a
m=`echo $a|sed 's/[0-9]//g'`
n=`echo $a|sed 's/[a-zA-Z]//g'`
if [ -n "$m" -a "$n" ];then
exit 0
fi
if [ -z $m ];then
echo digit
elif [ -Z $n ];then
echo alpha
exit 0
fi
2
#!/bin/bash
while :;
do
read -p "please input a number:" a
if [ $a -gt 5 ];then
exit 0
fi
done
3
#!/bin/bash
sum=0
for i in `seq 1 10`
do
if [ $(($i%2)) -eq 1 ];then
sum=$[$i+$sum]
fi
done
echo "the sum is: $sum"
4
#!/bin/bash
if [ $# -ne 2 ];then
echo "it is must input two number"
exit 0
fi
echo "the two number'sum is `expr $1 + $2`"
#!/bin/bash
read -p "please input zifu leixing:" a
m=`echo $a|sed 's/[0-9]//g'`
n=`echo $a|sed 's/[a-zA-Z]//g'`
if [ -n "$m" -a "$n" ];then
exit 0
fi
if [ -z $m ];then
echo digit
elif [ -Z $n ];then
echo alpha
exit 0
fi
2
#!/bin/bash
while :;
do
read -p "please input a number:" a
if [ $a -gt 5 ];then
exit 0
fi
done
3
#!/bin/bash
sum=0
for i in `seq 1 10`
do
if [ $(($i%2)) -eq 1 ];then
sum=$[$i+$sum]
fi
done
echo "the sum is: $sum"
4
#!/bin/bash
if [ $# -ne 2 ];then
echo "it is must input two number"
exit 0
fi
echo "the two number'sum is `expr $1 + $2`"
0
#! /bin/bash
name=`hostname`
IP=`ifconfig | grep "inet addr" | awk -F ":" '{print $2}'|cut -d " " -f 1|sed -n '1p'`
ver=`cat /etc/issue | sed -n '1p'`
kerver=`uname -r`
cat << EOF
hostname $name
IP $IP
version $ver
kernel $kerver
EOF
name=`hostname`
IP=`ifconfig | grep "inet addr" | awk -F ":" '{print $2}'|cut -d " " -f 1|sed -n '1p'`
ver=`cat /etc/issue | sed -n '1p'`
kerver=`uname -r`
cat << EOF
hostname $name
IP $IP
version $ver
kernel $kerver
EOF
0
1.编写shell脚本,判断用户输入的字符类型,是字母就输出alpha,是数字就输入digit。
2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。
3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。
4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。
5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。
- #!/bin/bash
- read -p "inout a alnum:" a
- [ `echo $a|sed 's/[[:alnum:]]//g'|wc -c` -ne 1 ] || [ -z $a ] || [ $a == ' ' ] && echo "fuck" && exit 3
- b=`echo $a|sed 's/[[:alpha:]]//g'|wc -c`
- c=`echo $a|sed 's/[[:digit:]]//g'|wc -c`
- if [ $b -eq 1 ];then
- echo "alpha"
- elif [ $c -eq 1 ];then
- echo "digit"
- else
- echo "Invalid Input"
- fi
2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。
- #!/bin/bash
- read -p "input a number:" a
- [ `echo $a|sed 's/[[:digit:]]//g'|wc -c` -ne 1 ] || [ -z $a ] || [ $a == ' ' ] && echo "fuck" && exit 3
- while [ $a -le 5 ];do
- read -p "input a number,again:" a
- done
3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。
- #!/bin/bash
- let sum=0
- for i in `seq 1 2 10`;do
- sum=$[$i+$sum]
- if [ $i -lt 9 ];then
- echo -n "$i,"
- else
- echo "$i"
- fi
- done
- echo "sum:$sum"
4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。
- #!/bin/bash
- let sum=0
- a () {
- sum=$[$1+$2]
- }
- [ $# -lt 2 ] && echo "input two number" && exit 3
- a $1 $2
- echo "$1+$2=$sum"
5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。
- #!/bin.bash
- z () {
- read -p "input a file name,absolute path:" a
- }
- m () {
- z
- ls $a &>/dev/null
- n=$?
- }
- m
- while [ $n -ne 0 ];do
- echo "The file does not exist."
- m
- done
- echo $PATH|grep `echo ${a%/*}` &>/dev/null
- n=$?
- [ $n -ne 0 ] && echo "Not found" && exit 3
- if [ $n -ne 0 ] || [ `ls -l $a|cut -c 10` != x ];then
- echo "Not found or not executable"
- else
- echo "Can find,executable"
- fi
0
这个是我自己做的,不是标准答案,调试过没有问题
#! /bin/bash
name=$(hostname)
pver=$(cat /etc/issue |sed -n '1'p)
kver=$(uname -r)
ip=$(ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d":" -f2)
echo "hostname $name"
echo "version $pver"
echo "kernel $kver"
echo "ip $ip"
#! /bin/bash
name=$(hostname)
pver=$(cat /etc/issue |sed -n '1'p)
kver=$(uname -r)
ip=$(ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d":" -f2)
echo "hostname $name"
echo "version $pver"
echo "kernel $kver"
echo "ip $ip"
0
本帖最后由 wuhen 于 2014-12-26 19:27 编辑
看看,最近考试有时间再做1、#!/bin/bash
1、read -p "input a charactor: " n
case $n in
[a-z]|[A-Z])
echo "alpha"
;;
[0-9])
echo "digit"
;;
*)
echo "input flase"
;;
esac
2、#!/bin/bash
n=1
while [ $n -le 4 ]
do
read -p "input a number: " n
m=`echo "$n"|sed 's/[0-9]//g'|wc -c`
if [ $m -ne 1 ]
then
echo "you input is not number"
exit
fi
done
echo "your input number is $n"
3、#!/bin/bash
sum=0
for i in `seq 1 10`
do
a=$[$i%2]
if [ $a -eq 1 ]
then
sum=$[$sum+$i]
fi
done
echo "odd sum is $sum"
4、#!/bin/bashsum=0
sum_a() {
sum=$[$a+$b]
echo "sum is $sum"
}
read -p "input two number: " a b
sum_a $a $b
看看,最近考试有时间再做1、#!/bin/bash
1、read -p "input a charactor: " n
case $n in
[a-z]|[A-Z])
echo "alpha"
;;
[0-9])
echo "digit"
;;
*)
echo "input flase"
;;
esac
2、#!/bin/bash
n=1
while [ $n -le 4 ]
do
read -p "input a number: " n
m=`echo "$n"|sed 's/[0-9]//g'|wc -c`
if [ $m -ne 1 ]
then
echo "you input is not number"
exit
fi
done
echo "your input number is $n"
3、#!/bin/bash
sum=0
for i in `seq 1 10`
do
a=$[$i%2]
if [ $a -eq 1 ]
then
sum=$[$sum+$i]
fi
done
echo "odd sum is $sum"
4、#!/bin/bashsum=0
sum_a() {
sum=$[$a+$b]
echo "sum is $sum"
}
read -p "input two number: " a b
sum_a $a $b
0
第五题答案:
#!/bin/bash
#written by wangtao 2014-12-24
while :;
do
read -p "Please input a filename: " name
if [ -z "$name" ];
then
echo "Please input a currect filename."
continue
fi
# a=`which locate 1>>/dev/null 2>&1;echo $?`
# if [ "$a" -ne "0" ];
# then
# yum install -y mlocate
# b=`echo $?`
# if [ "$b" -ne "0" ];
# then
# echo "sorry,It's fail.you should: yum install -y mlocate"
# exit
# fi
# fi
a=`find / -type f -name "$name" | wc -l`
if [ "$a" -eq "0" ];
then
echo "Please input a currect filename."
continue
fi
find / -type f -name "$name" > /usr/local/sbin/temp.txt
grep ".*" -n /usr/local/sbin/temp.txt > /usr/local/sbin/temp.log.txt
if [ "$a" -gt "1" ];
then
while :;
do
cat /usr/local/sbin/temp.log.txt
echo "Please Choose a number."
read -p "Your choose number is: " b
if [ -z "$b" ];
then
echo "Please choose a number 1."
continue
fi
b1=`echo $b | sed 's/[0-9]//g'`
if [ ! -z "$b1" ];
then
echo "Please choose a number 2."
continue
fi
if [ -z "$b1" ];
then
b2=`grep "$b" /usr/local/sbin/temp.log.txt | awk -F: '{print $2}'`
b3=`tail -1 /usr/local/sbin/temp.log.txt | awk -F: '{print $1}'`
if [ "$b3" -gt "$b" ];
then
echo "Please rechoose a number 3."
continue
fi
dir=`cat /usr/local/sbin/temp.txt`
echo $PATH >/usr/local/sbin/path.txt
n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`
if [ -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is execute and in the PATH"
exit
elif [ -x "$dir" ] && [ "$n" -ne "0" ];
then
echo "This file is execute ,but It's not in the PATH."
exit
elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is not execute, but It's in the PATH."
exit
fi
fi
done
fi
if [ "$a" -eq "1" ];
then
dir=`cat /usr/local/sbin/temp.txt`
echo $PATH >/usr/local/sbin/path.txt
n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`
if [ -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is execute and in the PATH"
exit
elif [ -x "$dir" ] && [ "$n" -ne "0" ];
then
echo "This file is execute ,but It's not in the PATH."
exit
elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is not execute, but It's in the PATH."
exit
fi
fi
done
#!/bin/bash
#written by wangtao 2014-12-24
while :;
do
read -p "Please input a filename: " name
if [ -z "$name" ];
then
echo "Please input a currect filename."
continue
fi
# a=`which locate 1>>/dev/null 2>&1;echo $?`
# if [ "$a" -ne "0" ];
# then
# yum install -y mlocate
# b=`echo $?`
# if [ "$b" -ne "0" ];
# then
# echo "sorry,It's fail.you should: yum install -y mlocate"
# exit
# fi
# fi
a=`find / -type f -name "$name" | wc -l`
if [ "$a" -eq "0" ];
then
echo "Please input a currect filename."
continue
fi
find / -type f -name "$name" > /usr/local/sbin/temp.txt
grep ".*" -n /usr/local/sbin/temp.txt > /usr/local/sbin/temp.log.txt
if [ "$a" -gt "1" ];
then
while :;
do
cat /usr/local/sbin/temp.log.txt
echo "Please Choose a number."
read -p "Your choose number is: " b
if [ -z "$b" ];
then
echo "Please choose a number 1."
continue
fi
b1=`echo $b | sed 's/[0-9]//g'`
if [ ! -z "$b1" ];
then
echo "Please choose a number 2."
continue
fi
if [ -z "$b1" ];
then
b2=`grep "$b" /usr/local/sbin/temp.log.txt | awk -F: '{print $2}'`
b3=`tail -1 /usr/local/sbin/temp.log.txt | awk -F: '{print $1}'`
if [ "$b3" -gt "$b" ];
then
echo "Please rechoose a number 3."
continue
fi
dir=`cat /usr/local/sbin/temp.txt`
echo $PATH >/usr/local/sbin/path.txt
n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`
if [ -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is execute and in the PATH"
exit
elif [ -x "$dir" ] && [ "$n" -ne "0" ];
then
echo "This file is execute ,but It's not in the PATH."
exit
elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is not execute, but It's in the PATH."
exit
fi
fi
done
fi
if [ "$a" -eq "1" ];
then
dir=`cat /usr/local/sbin/temp.txt`
echo $PATH >/usr/local/sbin/path.txt
n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`
if [ -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is execute and in the PATH"
exit
elif [ -x "$dir" ] && [ "$n" -ne "0" ];
then
echo "This file is execute ,but It's not in the PATH."
exit
elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
then
echo "This file is not execute, but It's in the PATH."
exit
fi
fi
done
0
本帖最后由 maria 于 2016-1-27 10:46 编辑
6
7
6
- #!/bin/bash
- sr=$1;
- m1=`echo $sr |sed 's/[^A-Z]//g'`;
- m2=`echo $sr |sed 's/[^a-z]//g'`;
- m3=`echo $sr |sed 's/[^0-9]//g'`;
- m4=`echo $sr |sed 's/[A-Za-z0-9]//g'`;
- pd(){
- N=0
- if [ "$m1" = "" ]
- then
- echo "输入必须含有大写字母";
- N=1;
- fi
- if [ "$m2" = "" ]
- then
- echo "输入必须含有小写字母";
- N=1;
- fi
- if [ "$m3" = "" ]
- then
- echo "输入必须含有数字";
- N=1;
- fi
- if [ "$m4" != "" ]
- then
- echo "输入不能含有符号";
- N=1;
- fi
- return $N;
- }
- pd;
- N=$?
- if [ $N -ne 0 ]
- then
- echo "输入不合法!";
- else
- echo "输入合法!";
- fi
7
- case “$1” in
- 1)echo Jan;;
- 2)echo Feb;;
- 3)echo Mar;;
- 4) echo Apr;;
- 5) echo May;;
- 6) echo Jun;;
- 7) echo Jul;;
- 8) echo Aug;;
- 9) echo Sep;;
- 10) echo Oct;;
- 11) echo Nev;;
- 12) echo Dec;;
- *) echo “must in rang 1-12”
- exit 1;;
编辑回复