bash入门脚本(未完善)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bash入门脚本(未完善)相关的知识,希望对你有一定的参考价值。
一、写一个脚本,实现如下功能:(1)显示/etc目录下所有已大写p或小写p开头的文件或目录本身:
(2)显示/var目录下的所有文件或目录本身,并将显示结果中的小字母转换为大写后显示:
(3)创建临时文件/tmp/myfile.xxx
~]nona test.sh
#!/bin/bash
ls -d /etc/[Pp]*
ls -d /var/* | tr [a-z] [A-Z]
mktemp /tmp/myfile.XXX
二、写一个脚本,通过命令行传递两个文件路径给脚本,计算器空白行数之和
vim /kbh.sh
#!/bin/bash
#
kb1=$(gerp "^$" $1 | wc -l)
kb2=$(gerp "^$" $2 | wc -l)
kbh=$[kb1+kb2]
echo $kbh 或 echo "kbh: $[$kb1+$kb2]"
三、通过参数传递一个用户名给脚本,此用户不存在时,添加之:
vim /useradd.sh
#!/bin/bash
if ! grep "^$1\>" /etc/passwd &> dev/null; then
useradd $1
echo $1 | passwd --stdin $1 &> /dev/null
echo "User Add $1 yes"
fi
vim /useradd.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo "AT least username"
exit 1
fi
if ! grep "^$1\>" /etc/passwd &> dev/null; then
useradd $1
echo $1 | passwd --stdin $1 &> /dev/null
echo "User Add $1 yes"
fi
vim /useradd.sh
#!/bin/bash
if [ $# -lt 1 ];then
echo "AT least username"
exit 1
fi
if grep "^$1\>" /etc/passwd &> dev/null; then
echo "User $1 exists"
exit 1
else
useradd $1
echo $1 | passwd --stdin $1 &> /dev/null
echo "User Add $1 yes"
fi
四、通过命令行参数给定两个数字,输出其中较大的数值;
vim /xxx.sh
#!/bin/bash
#
if [ $# -lt 2 ];then
echo "Tow integers."
exit 1
fi
if [ $1 -ge $2 ]; then
echo "Max number:$1"
else
echo "Max number: $2"
fi
vim /xxx.sh
#!/bin/bash
#
if [ $# -lt 2 ];then
echo "Tow integers."
exit 1
fi
declare -i max
if [ $1 -ge $2 ]; then
max=$1
else
max=$2
fi
echo "Max number: $max
vim /xxx.sh
#!/bin/bash
#
if [ $# -lt 2 ];then
echo "Tow integers."
exit 1
fi
declare -i max=$1
if [ $1 -ge $2 ]; then
max=$2
fi
五、通过命令行参数给定一个用户名,判断其ID号时偶数还是奇数;
vim /xxx.sh
#!/bin/bash
if [ $# -lt 1 ]; then
echo "userid X >1"
exit 1
fi
uid1=$(id -u $1)
let s=$[$uid1%2]
if [ $s -eq 0 ]; then
echo "user: $1 uid: $uid1 and OU"
else
echo "user: $1 uid: $uid1 and ji"
fi
vim /xxx.sh
#!/bin/bash
#
if [ $# -lt 1 ]; then
echo "userid Y > 1"
exit 1
fi
if id -u $1 &> /dev/null; then
uid=$(id -u $1)
let S=$[$uid%2]
if [ "$S" -eq 0 ]
then
echo "user: $1 uid: $uid an ou"
else
echo "user: $1 uid: $uid an ji"
fi
else
echo "wu yong hu"
fi
六、通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;都存在时返回每个文件的行数,并说明其中行数较多的文件’
#!/bin/bash
if [ $# -lt 2 ];then
echo "Wen Jian Shu > 2"
exit 1
fi
if [ -e $1 -a -e $2 ];then
w1=$(grep ^.*$ $1 | wc -l)
echo "Wenj: $1 HangShu: $w1"
w2=$(grep ^.*$ $2 | wc -l)
echo "Wenj: $2 HangShu: $w2"
if [ "$w1" -gt "$w2" ];then
echo "$1 Hang Shu Jiao Duo"
else
echo "$2 Hang Shu Jiao Duo"
fi
else
echo "WENJIAN: $1 or WenJian: $2 bu cun zai"
fi
七、
(1)传递一个参数给脚本,此参数为用户名;
(2)根据其ID好判断其用户类型:
0:管理员
1-999:系统用户
1000+:登录用户
#!/bin/bash
#
if [ $# -lt 1 ];then
echo "can chu xu lt 1"
exit 1
fi
if ! grep "^$1" /etc/passwd &> /home/cs;then
echo "wu cu y h"
exit 2
fi
idx=$(grep "^$1" /etc/passwd | cut -d : -f 3 )
if [ $idx -eq 0 ];then
echo "gly"
elif [ $idx -ge 1000 ];then
echo "dlyh"
else
echo "qtyh"
fi
八、
(1)列出如下菜单给用户
disk)show disks info;
mem)show memory info;
cpu)show cpu info;
*)quit
(2)提示用户给出自己的选择,而后显示对应其选择的相应系统信息
#!/bin/bash
#
cat << EOF
disk) show disks info
mem) show memory info
cpu) show cpu info
*) QUIT
EOF
read -p "Your choice: " options
if [[ "$options" == "disk" ]]; then
fdisk -l /dev/[sh]d[a-z]
elif [[ "$options" == "mem" ]]; then
free -m
elif [[ "$options" == "cpu" ]]; then
lscpu
else
echo "Unkown option."
exit 1
fi
九、循环遍历三个用户有则显示$username exits,没有则添加;
#/bin/bash
#
for username in user21 user22 user23; do
if id $username &>/etc/null; then
echo "$username exits"
else
useradd $username && echo "Add user $username dinlshed."
fi
done
十、创建1-10个空文件
#!/bin/bash
#
for filename in 1 2 3 4 5 6 7 8 9 10; do
if touch /tmp/f$filename
done
十一、求100以内所有正整数之和;
#!/bin/bash
#
declare -i sum=0
for i in {1..100}; do
sum=$[$sum + $i]
done
echo $sum
十二、判断/var/log目录下的每一个文件的内容类型
#!/bin/bash
#
for filename in /var/log/*; do
if [ -f $filename ];then
echo "Common file."
elif [ -d $filename ];then
echo "Dircory."
elif [ -L $filename ];then
echo "Symbolic link."
elif [ -b $filename ];then
echo "block special file."
elif [ -c $filename ];then
echo "character special file."
elif [ -S $filename ];then
echo "Socket file"
else
echo "Unkown."
fi
done
十三、分别求100以内分别求所有偶数之和,以及所有奇数之和;
#!/bin/bash
#
declare -i sunj=0
suno=0
for i in $( seq 1 2 100) ; do
sunj=$[$sunj + $i]
done
echo $sunj
for x in $(seq 0 2 100); do
suno=$[$suno + $x ]
done
echo $suno
十四、计算当前系统所有用户的id之和;
#!/bin/bash
for i in `cut -d: -f3 /etc/passwd`;do
sum=$[$sum+$i]
done
echo $sum
十五、通一过脚本参数传递一个目录给脚本,而后计算此目录下所有文本文件的行数之和;并说明此类文件的总数;
#!/bin/bash
read -p "Please enter your directory:" file
[ -z $file ] && echo “error" && exit 3
for i in `find $file -type f`;do
wc=`cat $i | wc -l`
for i in $wc;do
sum=$[$sum+$i]
done
done
echo $sum
以上是关于bash入门脚本(未完善)的主要内容,如果未能解决你的问题,请参考以下文章