天天看點

shell---practice2

要求:

       1.設定變量file的值為/etc/passwd

       2.使用循環讀取檔案/etc/passwd的第2,4,6,10,13,15行,并顯示其内容

       3.把這些行儲存至/tmp/mypasswd檔案中

#!/bin/bash
#File: for_dir.sh
#Date: 2016-01-12

changeFile="/tmp/mypasswd"     //定義changFile變量的值

if [ ! -f $changeFile ]; then     //判斷changFile是否存在
        touch $changeFile
else
        echo "The $changeFile is exist."
fi

#if [ ! -x $changeFile ]; then     //此if語句為賦予權限,可以省略     
#        chmod +x $changeFile
#else
#        echo "The $changeFile with executable permissons."
#fi

file="/etc/passwd"      //定義file變量的值

for i in 2 4 6 10 13 15     //将i做for循環,依次指派2 4 6 10 13 15
do
        line=`sed -n "$i"p $file`     //取出對應的行,并指派給變量line
        echo "$line"
        echo "$line" >> /tmp/mypasswd     
done