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
(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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.