shell脚本简单例子
2024-08-30 09:39:11
eg:
Expect:
1.用环境变量RANDOM随机生成一个100以内的随机数
2.read读取当前输入
3.当前输入对比随机生成的数
4.当两个数相等时跳出苏循环,并计数(比较n次结果才相等)
#!/bin/bash
i=
num=$(expr $RANDOM % )
echo $num
while true
do
let i++
read -p "Please input number(1-100):" digit
if [ $digit -eq $num ]; then
echo $digit
echo "一共$i 次!!"
break
elif [ $digit -lt $num ]; then
echo "The number is less!"
elif [ $digit -gt $num -o $digit -gt ]; then
echo "The number is bigger"
#elif [ $digit -gt 100 ]; then
# echo "not in the range!! Please reinput."
else
continue
fi
done
eg:
复习简单实例 , 简单输入n次要取得值,最后打印出获得的所有值
#!/bin/bash
attr=()
num=
while true
do
read -p ">>input:" name
attr[$num]=$name
echo ${attr[$num]}
let num++
if [ $num -eq ]; then
echo ${attr[*]}
exit
fi
done
eg:for循环
eg:continue和break的区别
continue结束本次循环,进行下一次循环
break跳过本次循环
#!/bin/bash
for i in `seq `
do
if [ $i -eq ]; then
continue
else
echo $i
fi
done #!/bin/bash
int=
while :
do
let int++
if [ $int -lt ]; then
echo $int
else
break
fi
done
====================
for循环的用法
#!/bin/bash
for ((a=;a<;a++))
do
echo $a
done
====================
嵌套循环
#!/bin/bash
for ((a=;a<;a++))
do
echo "outer loop: $a"
for ((b=;b<;b++))
do
if [ $b -eq ]; then
continue
else
echo "inner loop: $b"
fi
done
done
====================
shell脚本编写添加用户,随机密码后五位
#!/bin/bash
for ((i=;i<;i++))
do
password=`openssl rand -hex | cut -c1-`
echo "$password"
cat /etc/passwd|grep -qa user$i
if [ $? -eq ]; then
echo "user${i} already"
continue
else
useradd user$i;echo $a|passwd --stdin user$i
fi
done
最新文章
- 动态添加LinearLayout的高度
- 利用git+hugo+markdown 搭建一个静态网站
- TCP协议与UDP协议的区别
- 【OpenGL】 第一篇 OpenGL概览
- ruby 学习笔记 1
- 【原】redux学习笔记
- sublime文档
- Unity项目在亚马逊KindleFire设备上旋转不正确的解决方案
- Centos环境下部署游戏服务器-Eclipse
- c++之函数原型
- JAVA-应用easyui
- Githut Token (hidden): Githut 安装验证
- gridcontrol显示行号,总行,打印,导出Excel,设置标头及内容居中方法
- SQL Server实现远程访问
- dt转换List CovertListHelper
- linux无法启动httpd服务问题
- 《计算机网络》-CCNA命令大全
- natapp 穿透访问 vue项目 Invalid Host header
- (转)SQL知识_SqlParameter数组
- Global.asax 详解