码迷,mamicode.com
首页 > 其他好文 > 详细

bash脚本简单脚本集合(1)

时间:2016-09-08 16:40:37      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:for   bash   国际棋盘   

1、使用一个用户名作为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
#
read -p "please input one UserName: " USERNAME
id $USERNAME &>/dev/null
if [ $? -eq 0 ];then
echo "The User is exist"
exit
else
useradd $USERNAME
echo "$USERNAME already add"
echo "`grep "\<$USERNAME\>" /etc/passwd`"
fi


2、判断用户输入文件路径,显示其文件类型(普通、目录、链接,其他文件类型)

#!/bin/bash
#
read -p "please input valid path: " PATH1
[ ! -e $PATH1 ] && echo "This path is non-exsit" && exit
type1=`ls -ld $PATH1 | grep -o "^."`
case $type1 in
-)
echo "This is file"
;;
d)
echo "This is directory"
;;
l)
echo "This is links"
;;
p)
echo "This is pipe"
;;
s)
echo "This is sokect"
;;
c)
echo "This is charater"
;;
b)
echo "This is block"
;;
esac


3、添加10个用户user1-user10,密码同用户名

#!/bin/bash
#
for i in {1..10}
do
id user$i &> /dev/null
if [ $? -eq 1 ];then
  useradd user$i
echo "user$i" | passwd --stdin user$i &> /dev/null
echo "The User$i success add!"
else
  echo "The User$i is exist"
fi
done


4、使用for循环打印九九乘法表

#!/bin/bash
#
for h in {1..9}
 do
  for l in `seq 1 $h`
   do
squ=$(($h*$l))
echo -ne "${h}x${l}=$squ\t"
  done
 echo
done


5、使用for循环打印国际象棋棋盘

#!/bin/bash
#
for j in {1..8}
  do
for i in {1..8}
  do
if [ $[($i+$j)%2] -eq 1 ];then
  echo -ne "\e[41m  \e[0m"
else
  echo -ne "\e[47m  \e[0m"
fi
done
   echo ""
done


bash脚本简单脚本集合(1)

标签:for   bash   国际棋盘   

原文地址:http://simatiezhu.blog.51cto.com/11892729/1850586

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!