小腳本練習

腳本練習:

傳遞一個用戶名參數給腳本,判斷此用戶的用戶名跟其基本組的組名是否一致,並將結果顯示出來。

寫一個腳本:

#!/bin/bash

USER=$1

if [ ! $# -eq 1 ];then

echo "please input only one username "

exit 2

fi

if ! id $1 &> /dev/null;then

echo "the user is not exist"

exit 3

fi

if [ id -u $1 -eq id -g $1 ];then

echo "same"

else

echo "different"

fi

判斷當前主機的CPU生產商,其信息在/proc/cpuinfo文件中vendor id一行中。

如果其生產商爲AuthenticAMD,就顯示其爲AMD公司;

如果其生產商爲GenuineIntel,就顯示其爲Intel公司;

否則,就說其爲非主流公司;

#!/bin/bash

CPUFAC=sed -n '/^vendor_id/p' /proc/cpuinfo | cut -d' ' -f2

if [ $CPUFAC == AuthenticAMD ];then

echo "AMD"

elif

[ $CPUFAC == GenuineIntel ];then

echo "Inter"

else

echo"other company"

fi

寫一個腳本:

給腳本傳遞三個整數,判斷其中的最大數和最小數,並顯示出來。

#!/bin/bash

declare MAXNUM

declare MINNUM

if [ $# -ne 3 ];then

echo "please input three number"

exit 2

fi

if [ $1 -ge $2 ];then

MAXNUM=$1

MINNUM=$2

else

MAXNUM=$2

MINNUM=$1

fi

if [ $MAXNUM -ge $3 ];then

echo "the maxnum is $MAXNUM"

[ $1 -ge $2 ] && echo "is first number" || echo "is second number"

else

MAXNUM=$3

echo "the maxnum is $MAXNUM is third number"

fi

if [ $MINNUM -le $3 ];then

 [ $1 -le $2 ]&& echo "the minnum is $1" || echo "the minnum is $2 "

[ $1 -le $2 ]&& echo "the minnum is first" || echo "the minnum is second"

else

echo "the minnum is $3 is third number "

fi

測試字符串

1 [ $A == $B ] 此時的==前後都要接空白符 不然默認會識別成賦值,

2 [ $A != $B ]

3測試字符串是否爲空的單目運算符

-n string 空爲真,不爲空爲真 爲空時爲假 且測試時 [空格變量空格] 這樣的格式進行測試

4測試字符串不爲空

-s 不爲空爲真 ,空爲假

4測試字符串是否大於 小於 > < 來表示

for 循環的使用

for 變量 in 列表 ;do

命令行1;

命令行2;

done

1.1生成列表

1.1{1..100}

1.2 seq 起始數 步長 結束數

seq number 表示從1開始到number

seq number1 number2 表示步長爲1 起始數爲number1 結束數爲number2

seq number1 number2 number3 1表示起始數 2表示步長 3表示結束數

1.3 ls /etc/

數據類型的申明 declare

declare -i SUM=0 申明爲整數!

declare -x 表示申明爲環境變量

寫一個腳本:

1、設定變量FILE的值爲/etc/passwd

2、依次向/etc/passwd中的每個用戶問好,並顯示對方的shell,形如:

Hello, root, your shell: /bin/bash

3、統計一共有多少個用戶

4、只向默認shell爲bash的用戶問聲好

cat /etc/passwd | cut -d: -f1,7 | head -1 | tail -1 | cut -d: -f2 | sed 's@/.*/@@'

cat /etc/passwd | cut -d: -f1,7 | head -1 | tail -1 | cut -d: -f1

#!/bin/bash

declare I

declare J=0

NUM=cat /etc/passwd | wc -l

for I in seq $NUM ;do

USER=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f1`

USHELL=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f2 | sed 's@/.*/@@'`

echo "HI $USER your shell is $USHELL"

if [ $USHELL == bash ];then

   echo "hi $USER your shell is $USHELL"

  let J=$J+1

fi

done

echo "the users total is $I the bash is $J"

exit

寫一個腳本:

1、添加10個用戶user1到user10,密碼同用戶名;但要求只有用戶不存在的情況下才能添加;

擴展:

接受一個參數:

add: 添加用戶user1..user10

del: 刪除用戶user1..user10

其它:退出

adminusers user1,user2,user3,hello,hi

!/bin/bash

if [ $1 == "add" ];then

for I in {1..10};do

 if  id user$I &> /dev/null;then

   echo "user$I is exist"

else

   useradd user$I

   echo "user$1" | passwd --stdin user$I &> /dev/null

fi

done

elif [ $1 == "del" ];then

 for I in {1..10};do

 userdel -r user$I

 done

else

echo "please input add or del "

exit 2;

fi

寫一個腳本,分別顯示當前系統上所有默認shell爲bash的用戶和默認shell爲/sbin/nologin的用戶,並統計各類shell下的用戶總數。顯示結果形如:

BASH,3users,they are:

root,redhat,gentoo

NOLOGIN, 2users, they are:

bin,ftp

#!/bin/bash

#

NUMBASH=grep "bash$" /etc/passwd | wc -l

BASHUSERS=grep "bash$" /etc/passwd | cut -d: -f1

BASHUSERS=echo $BASHUSERS | sed 's@[[:space:]]@,@g'

echo "BASH, $NUMBASH users, they are:"

echo "$BASHUSERS

組合測試條件

-a: 與關係

-o: 或關係

!: 非關係

if [ $# -gt 1 -a $# -le 3 ]

if [ $# -gt 1 ] && [ $# -le 3 ]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章