linux shell学习 记录2
#!/bin/bash
#判断输入的是什么
read -t 30 -p "please input a filename: " file
if [ -z "$file" ]
then
echo "请输入..."
exit 1
#-e 是否文件
elif [ ! -e "$file" ]
then
echo "请输入文件名"
exit 2
elif [ -f "$file" ]
then
echo "$file 是一个文件"
elif [ -d "$file" ]
then
echo "$file 是目录"
else
echo "$file 是其它文件"
fi
#!/bin/bash
#case demo
read -t 30 -p "please input yes/no: " cho
case "$cho" in
"yes")
echo "yes exec...."
;;
"no")
echo "no exit....."
;;
*)
echo "乱输入的...."
;;
esac
#!/bin/bash
#for demo
for i in 1 2 3 4 5
do
echo $i
done
#!/bin/bash
#for 解压文件
cd /home/shtest/shell2/
ls *.tar.gz > ls.log
ls *.tgz >> ls.log
for i in $( cat ls.log )
do
tar -zxf $i &>/dev/null #所有输出信息都不看
#tar -zxf $i
done
rm -f ls.log
#!/bin/bash
s=0;
for (( i=1;i<=100;i=i+1 ))
do
s=$(( $s+$i ))
done
echo $s
#!/bin/bash
#批量添加用户
read -p "please input user name: " -t 30 name
read -p "please input the number of users: " -t 30 num
read -p "please input the password of user: " -t 30 pass
#-z判断是否为null
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
then
#把num 的数字替换为null
y=$(echo $num | sed 's/[0-9]//g')
if [ -z "$y" ] #是否为空
then
for ((i=1;i<$num;i=i+1 ))
do
#添加用户
/usr/sbin/useradd $name$i &>/dev/null
#设置密码
echo $pass | /usr/bin/passwd --stdin $name$i &>/dev/null
done
fi
fi
#!/bin/bash
#删掉所有普通用户 -v 取返 cut -d 折分 取第一个
use=$(cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f1)
for i in $use
do
# -r 连家目录一起删除
userdel -r $i
done
#!/bin/bash
#while demo
i=1
s=0
# le 小于等于100
while [ $i -le 100 ]
do
s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "the sum: "$s
#!/bin/bash
#until demo
i=1
s=0
#和while 相返 直到变量i大于100 就停止
until [ $i -gt 100 ]
do
s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "the sum: " $s