bash腳本示例2

1、編寫腳本計算/etc/passwd文件中第10個用戶和第20個用戶id號之和。


#!/bin/bash

#

a=$(head /etc/passwd | tail -1 | cut -d : -f 3)

b=$(head -20 /etc/passwd | tail -1 | cut -d : -f 3)

echo $[a+b]


2、將當前主機名保存至hostName變量中,主機名如果爲空,或者爲localhost.localdomain,則將其設置爲www.magedu.com


#!/bin/bash

#

hostName=$(hostname)

[ -z $hostName -o $hostName == localhost.localdomain -o $hostName == localhost ] && hostname www.magedu.com


3、編寫腳本,通過命令行參數傳入一個用戶名,判斷uid是偶數還是奇數。


#!/bin/bash

#

uid=$(grep "^$1\>" /etc/passwd | cut -d : -f 3)

remainder=$[$uid % 2]


if [ $remainder == 0 ]; then

    echo "The uid of user $1 is even."

else

    echo "The uid of user $1 is odd."

fi


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