shell脚本练习题(一)

写一个脚本
1.设定变量FILE的值为/etc/passwd
2.依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么
形如:(提示:LINE=wc -l /etc/passwd | cut -d" " -f1)
Hello,root,your UID is 0.
3.统计一个有多少个用户

方案1#!/bin/bash
FILE="/etc/passwd"
LINE=`cat /etc/passwd|wc -l`

for i in $(seq 1 $LINE)
    do
    NAME=`cat /etc/passwd | awk -F ":" '{printf $1"\n"}'|sed -n ${i}p`     利用awk和sed截取出所需用的值
    UI=`cat /etc/passwd | awk -F ":" '{printf $3"\n"}'|sed -n ${i}p`
    echo Hello,$NAME,your UID is $UI
done
    echo there are $LINE users
方案2#!/bin/bash
file="/etc/passwd"
    LINES=`wc -l $file | cut -d" " -f1` 
                    ##利用cut截取出需用的值
           for I in `seq 1 $LINES`;do 

           userid=`head -$I $file | tail -1 |cut -d: -f3` 

           username=`head -$I $file | tail -1 |cut -d: -f1` 

           echo "hello $username,your UID is $userid" 

           done 

           echo "there are $LINES users"

你可能感兴趣的:(shell脚本练习题(一))