一些初学shell自己写的一些练习题脚本
1斐波拉契数列前10个
#!/binbash
#
declare A=0
declare B=1
for ((i=1;i<6;i++)); do
let A+=B
printf "%-5s" $A
let B+=A
printf "%-5s" $B
done
printf "\n"
=====================================================================================================================
2、添加用户 ,并且询问是否输入密码,并判断密码的复杂度(包括长度和是否在字典里的常用字符)输入密码确认,创建成功提示。
#!/bin/bash
#
declare A=1
read -p "please input user: " USER
while [ -z $USER ]; do
read -p "please input user: " USER
done
if [ $USER == "quit" ] ; then
exit 0
fi
while id $USER &> /dev/null ; do
echo "$USER exist"
read -t 8 -p "again input user: " USER
done
useradd $USER &> /dev/null
read -t 8 -p "do you want create password:(y|n): " CHOICE
case $CHOICE in
y|yes|Y|YES)
read -p "please input your password: " PASSWORD
until [ "$(echo $PASSWORD | sed "s/ //g" | wc -L)" -gt 5 ] ; do
read -p "password short,please again input: " PASSWORD
done
while grep "$PASSWORD" /usr/share/dict/words &> /dev/null; do
read -p "this password is sight words. again input password: " PASSWORD
done
read -p "again input password: " PASSWORD2
until [ $PASSWORD == $PASSWORD2 ]; do
read -p "twice password not atypism,please again input: " PASSWORD2
done
echo $PASSWORD | passwd --stdin $USER &> /dev/null
echo "$USER create finished"
;;
n|N|no|NO)
echo "$USER Create success,but not set password"
exit 10
还存在写问题,要进一步修改下
============================================================================================================================
3、小行星 ,形如
*
***
*****
*******
*********
#!/bin/bash
#
for I in `seq 1 2 9`; do
for ((M=0;M<=$[(9-$I)/2];M++));do
echo -n " "
done
for ((J=0;J<I;J++)); do
echo -n "*"
done
for ((K=0;K<=$[9-$J];K++)); do
echo -n " "
done
for ((M=0;M<=$[(9-$I)/2];M++));do
echo -n " "
done
echo -e "\n"
done
=======================================================================================================================
模拟系统登陆
#!/bin/bash
#
user1(){
while [ -z $USER ];do
echo -e "\nRed Had Enterprise Linux server 5.8 \nkernel 2.6.18-308.e15 on an x86_64\n"
read -p "`hostname | awk -F. '{print $1}'`login: " USER
done
}
user2(){
while [ -z $USER ]; do
read -p "longin: " USER
done
}
passwd1(){
if [ -z $PASSWORD ]; then
echo "Login incorrect"
exit 10
fi
}
login(){
if grep "$USER" /root/practice/user &>/dev/null && grep "$PASSWORD" /root/practice/passwd &>/dev/null ; then
echo -e "\nlast longin: `date` from `ifconfig eth0 | awk -F: '/inet addr/{print $2}' | sed 's/Bcast//g'`"
exit 0
fi
}
read -p "`hostname | awk -F. '{print $1}'`login: " USER
user1
read -t 20 -s -p "passwd: " PASSWORD
passwd1
login
user2
==================================================================================================
5,数字游戏
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
#!/bin/bash
#
declare A=0
for ((I=1;I<=9;I++)); do
printf "$A\n\n"
A=$I$A$I
done
=======================================================================================
6,输出乘法口诀表
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
#!/bin/bash
#
for ((i=1;i<10;i++)); do
for((j=1;j<($i+1);j++)); do
printf "%-10s" "$j""x""$i""=""$[$j*$i]"
done
printf "\n"
done
=====================================================================================================
7,随便给一组数字从大到小排序
#!/bin/bash
#
declare j=0
declare C=0
for I in $*;do
a[j]=$I
let j++
done
for ((A=0;A<$#;A++));do
for ((K=$[$A+1];K<$#;K++)); do
if [ ${a[$K]} -gt ${a[$A]} ]; then
C=${a[$K]}
a[$K]=${a[$A]}
a[$A]=$C
fi
done
done
echo ${a[*]}
另一个 显示从大到小 ,从小到大
#!/bin/bash
declare i=0
for I in $*; do
a[i]=$I
let i++
done
for ((i=0;i<${#a[*]};i++));do
for ((j=$[$i+1];j<${#a[*]};j++));do
if [ ${a[j]} -gt ${a[i]} ]; then
C=${a[j]}
a[j]=${a[i]}
a[i]=$C
fi
done
done
echo "max to smaill: ${a[*]}"
echo -n "smaill to max: "
for ((i=$#;i>=0;i--)); do
printf "%s " "${a[i]}"
done
printf "\n"
~
~
==========================================================================================
8,10个随机数 找找出最大数和最小数
#!/bin/bash
#
declare -i A=0
for I in {1..10};do
declare -i NUMBER=`echo $RANDOM`
[ $I -eq 1 ] && declare -i SMAILL=`echo $RANDOM`
if [ $I -le 9 ]; then
echo -n "$NUMBER,"
else
echo $NUMBER
fi
[ $NUMBER -lt $SMAILL ] && SMAILL=$NUMBER
[ $NUMBER -gt $A ] && A=$NUMBER
done
echo "max number is $A"
echo "smaill number is $SMAILL"
~ ==================================================================================================
10,查看硬盘和内存使用率,可以选择显示什么如下
[root@bogon ok]# sh diskusage.sh
m|M) show memory utilication.
s|S) show swap utilication.
d|D) show disk utilication.
q|Q) quit..
please choice:
#!/bin/bash
#
LIST(){
cat << EOF
m|M) show memory utilication.
s|S) show swap utilication.
d|D) show disk utilication.
q|Q) quit..
EOF
}
LIST
read -p "please choice: " OPTION
until [ $OPTION == "Q" -o $OPTION == "q" ] ; do
case $OPTION in
m|M)
free -m | grep "Mem";;
s|S)
free -m | grep "Swap";;
d|D)
df -lh ;;
*)
LIST
read -p "option error .please again choice: " OPTION
continue
;;
esac
LIST
read -p "please again choice" OPTION
done
~
=======================================================================================
还有好多,慢慢写上来。待续。。。。。
输入用户,判断是否输入为空,输入Q|q 则退出,判断用户是否存在,不存在继续输入,存在则给出选择列表,显示相应的选择,然后让后继续输入,直到输入Q|q
#!/bin/bash
#
read -p "please input user: " USER
while [ -z $USER ]; do
read -p "please input user: " USER
done
[ $USER == 'q' -o $USER == 'Q' ] && exit 0
until id $USER &>/dev/null ; do
read -p "$USER not exist again input: " USER
[ $USER == 'q' -o $USER == 'Q' ] && exit 0
done
cat << EOF
#!/bin/bash
#
read -p "please input user: " USER
while [ -z $USER ]; do
read -p "please input user: " USER
done
[ $USER == 'q' -o $USER == 'Q' ] && exit 0
until id $USER &>/dev/null ; do
read -p "$USER not exist again input: " USER
[ $USER == 'q' -o $USER == 'Q' ] && exit 0
done
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "your choice: " CHOICE
while [ -z $CHOICE ] ; do
read -p "your choice: " CHOICE
done
until [ $CHOICE == "q" -o $CHOICE == "Q" ] ; do
case $CHOICE in
u|U)
echo "$USER UID is `id -u $USER`"
read -p "your choice: " CHOICE
;;
G|g)
echo "$USER GID is `id -g $USER`"
read -p "your choice: " CHOICE
;;
S|s)
echo "$USER SHELL is `grep "$USER\>" /etc/passwd | awk -F: '{print $NF}'`"
read -p "your choice: " CHOICE
;;
*)
read -p "choice error please again your choice: " CHOICE
;;
esac
done
一些初学shell自己写的一些练习题脚本的更多相关文章
- 小鸟初学Shell编程(二)编写简单的Shell脚本
Shell脚本 编写Python.PHP脚本通常需要掌握语言的函数,那么Shell脚本则不需要,只需要掌握Linux命令就可以编写Shell脚本,因为Shell脚本就是由多个Linux命令组成,通过将 ...
- 小鸟初学Shell编程(三)脚本不同执行方式的影响
执行命令的方式 执行Shell脚本的方式通常有以下四种 方式一:bash ./test.sh 方式二:./test.sh 方式三:source ./test.sh 方式四:. ./test.sh 执行 ...
- python+paramiko库+svn写的自动化部署脚本
第一篇博文 直接开门见山的说了. 这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞. 一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一 ...
- 如何写SysV服务管理脚本
本文目录: 1.1 SysV脚本的特性1.2 SysV脚本要具备的能力1.3 start函数分析1.4 stop函数分析1.5 reload函数分析1.6 status.restart.force-r ...
- centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/index.php <<EOF重定向 shell的变量和函数命名不能有横杠 平台可以用arch命令,获取是i686还是x86_64 curl 下载 第三十九节课
centos shell编程5 LANMP一键安装脚本 lamp sed lnmp 变量和字符串比较不能用-eq cat > /usr/local/apache2/htdocs/ind ...
- 写一个php小脚本辅助渗透测试
因为一个注入要爬行一些数据,然后写的一个小脚本,能写脚本来辅助渗透,也算是里程碑.哈哈哈 <?php $num = 1; while ($num <= 39) { $web_url = & ...
- Shell实现多级菜单系统安装维护脚本实例分享
Shell实现多级菜单系统安装维护脚本实例分享 这篇文章主要介绍了Shell实现多级菜单系统安装维护脚本实例分享,本文脚本用多级菜单实现管理WEB服务器.Mysql服务器.Nginx服器等,需要的朋友 ...
- 使用 PHP + shell 生成 一键设置权限的脚本。
linux 系统 支持PHP脚本一键设置环境.shell脚本一键设置环境.那么 我今天 使用 PHP + shell 生成 一键设置权限的脚本. 举例子:linux服务器 一键配置discuz网站环 ...
- shell编写一个批量添加用户脚本
shell编写一个批量添加用户脚本 5.1问题 本例要求在虚拟机server0上创建/roo ...
随机推荐
- Myeclipse中文乱码解决方式
我们刚刚安装的Myeclipse有可能使用的是GBK的编码方式,而通常我们的程序都是使用的是UTF-8的编码方式,所以当我们导入一个项目的时候,会产生乱码,解决方式如下: 一.将整个project设置 ...
- mongodb-2.6.0 在win7 64下的安装和服务启动
转自: http://blog.csdn.net/lingchen214/article/details/24537629 1 自定义安装到C:\mongodb目录下. 2 手动在C:\mong ...
- 子元素绝对定位absolute后,自动撑开宽度
position: absolute; white-space: nowrap;
- oracle 任务使用
文章访问地址:http://www.cnblogs.com/hoojo/p/oracle_procedure_job_interval.html
- [转]hadoop2.x常用端口及定义方法
端口 Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集 ...
- SVN使用—高级用法
一.SVN分支 Branch 选项会给开发者创建出另外一条线路.当有人希望开发进程分开成两条不同的线路时,这个选项会非常有用. 情景: 比如项目 demo 下有两个小组,svn 下有一个 trunk ...
- git报错--RPC failed; curl 18 transfer closed with outstanding read data remaining
遇到的问题一: error: RPC failed; curl 18 transfer closed with outstanding read data remaining fata ...
- shell检查网络出现异常、僵尸进程、内存过低后,自动重启
#!/bin/bash while : do neterror=$(/bin/netstat -a | grep -cw "CLOSE_WAIT") echo "get ...
- shell脚本小示例
求1-100自然数的和: 法一:for循环 #!/bin/bash # declare -i sum=0 for ((i=0;i<=100;i++));do let sum+=$i done e ...
- Centos/ubuntu配置SVN服务
Centos安装svn yum -y install subversion ubuntu安装svn apt-get install subversion Centos配置svn root@hello: ...