写一个脚本:要求如下:
1:依次向/etc/passwd目录中的每一个用户问好,并指出其UID号!(eg:HELLO,root,your uid is 0)
2:统计这个脚本共有多少个账户?
参考脚本:
{{{密码回复可见}}}
1:依次向/etc/passwd目录中的每一个用户问好,并指出其UID号!(eg:HELLO,root,your uid is 0)
2:统计这个脚本共有多少个账户?
参考脚本:
{{{密码回复可见}}}
0
cut:
awk:
- #/bin/bash
- #2015-12-14
- #balich
- file="/etc/passwd"
- Line=`wc -l $file |cut -d " " -f1`
- for i in `seq 1 $Line`; do
- username=`head -$i $file | tail -1 |cut -d: -f1`
- userid=`head -$i $file |tail -1 |cut -d: -f3`
- echo "hello $username,your UID is $userid"
- done
- echo "there are $Line user"
awk:
- #/bin/bash
- #2015-12-14
- #balich
- file="/etc/passwd"
- Line=`wc -l $file |awk -F ' ' '{print $1}'`
- for i in `seq 1 $Line`; do
- username=`head -$i $file | tail -1 |awk -F ':' '{print $1}'`
- userid=`head -$i $file |tail -1 |awk -F ':' '{print $3}'`
- echo "hello $username,your UID is $userid"
- done
- echo "there are $Line user"
0
其实,我也不太熟,也是新手!就是自己找些练习题,练练,自己想想,再练!
agh353272297 发表于 2015-12-13 19:35
想了解下,你是如何学习shell脚本这块内容的呢。 我对这块学习起来感觉很费力。能指点下?
其实,我也不太熟,也是新手!就是自己找些练习题,练练,自己想想,再练!
0
- #!/bin/bash
- 4
- 3 users=`awk -F':' '{print $1}' /etc/passwd`
- 2 uid=`awk -F':' -v a=$i '$1==a{print $3}' /etc/passwd`
- 1 for i in $users;do
- 6 echo "HELLO, $i,your uid is `awk -F':' -v a=$i '$1==a{print $3}' /etc/passwd`"
- 1 done
0
#!/bin/bash
name=`cat /etc/passwd |awk -F: '{print $1}'`
for u in $name
do
i=`id $u |awk -F'=' '{print $2}' |awk -F'(' '{print $1}'`
uid_num=`grep $u:x /etc/passwd |awk -F: '{print $3}'`
if [ $i == $uid_num ]
then
echo "Hello $u, your uid is $i !"
else
echo "Wrong!!!"
fi
done
y=`cat /etc/passwd |wc -l`
echo "There are $y users."
name=`cat /etc/passwd |awk -F: '{print $1}'`
for u in $name
do
i=`id $u |awk -F'=' '{print $2}' |awk -F'(' '{print $1}'`
uid_num=`grep $u:x /etc/passwd |awk -F: '{print $3}'`
if [ $i == $uid_num ]
then
echo "Hello $u, your uid is $i !"
else
echo "Wrong!!!"
fi
done
y=`cat /etc/passwd |wc -l`
echo "There are $y users."
0
本帖最后由 Landon 于 2016-1-3 17:50 编辑
我使用了数组存放的方式
进行了用户的排列 从0开始上升!如果只是打印不好玩!
#!/bin/bash
uid=`cat /etc/passwd|awk -F ':' '{print $3 ,$1}'|sort -n|awk '{print $1}'`
user=`cat /etc/passwd|awk -F ':' '{print $3 ,$1}'|sort -n|awk '{print $2}'`
sum=0
for a in $user
do
((sum=$sum+1))
us[$sum]=$a
done
n=`echo $sum`
sum2=0
for b in $uid
do
((sum2=$sum2+1))
ui[$sum2]=$b
done
for i in `seq 1 $n`
do
echo "Hello ,UID:${ui[$i]}, USER :${us[$i]}"
donenum=`wc -l /etc/passwd`
echo "total account $num"
我使用了数组存放的方式
进行了用户的排列 从0开始上升!如果只是打印不好玩!
#!/bin/bash
uid=`cat /etc/passwd|awk -F ':' '{print $3 ,$1}'|sort -n|awk '{print $1}'`
user=`cat /etc/passwd|awk -F ':' '{print $3 ,$1}'|sort -n|awk '{print $2}'`
sum=0
for a in $user
do
((sum=$sum+1))
us[$sum]=$a
done
n=`echo $sum`
sum2=0
for b in $uid
do
((sum2=$sum2+1))
ui[$sum2]=$b
done
for i in `seq 1 $n`
do
echo "Hello ,UID:${ui[$i]}, USER :${us[$i]}"
donenum=`wc -l /etc/passwd`
echo "total account $num"
0
#!/bin/bash c=`cat /etc/passwd|wc -l` for i in `seq 1 $c`;do
user=`sed -n "$i"p /etc/passwd|awk -F ':' '{print $1}'`
uid=`sed -n "$i"p /etc/passwd|awk -F ':' '{print $3}'`
echo "Hello,$user your uid is $uid."
done
echo $c
exit 0
user=`sed -n "$i"p /etc/passwd|awk -F ':' '{print $1}'`
uid=`sed -n "$i"p /etc/passwd|awk -F ':' '{print $3}'`
echo "Hello,$user your uid is $uid."
done
echo $c
exit 0
0
本帖最后由 wsw13640218682 于 2016-1-30 02:00 编辑
#!/bin/bash
a="/etc/passwd"
b="/root/b"
if awk -F: '{print "hello," $1"," " your uid is " $3 }' $a > $b
then
cat $b
awk '{print $2}' $b |wc -l
fi
#!/bin/bash
a="/etc/passwd"
b="/root/b"
if awk -F: '{print "hello," $1"," " your uid is " $3 }' $a > $b
then
cat $b
awk '{print $2}' $b |wc -l
fi
0
- #!/bin/bash
- num=0
- cat /etc/passwd | while read line
- do
- user=`echo -n $line | awk -F ':' '{print $1}'`
- uid=`echo -n $line | awk -F ':' '{print $3}'`
- echo "HELLO,${user},your uid is ${uid}!"
- ((num++))
- echo $num
- done
- echo "There is ${num} users!"
0
本帖最后由 a57601247 于 2016-3-28 18:01 编辑
#!/bin/bash
File="/etc/passwd"
for i in `cat $File`;
do
user=`echo $i | cut -d ':' -f 1`
uid=`echo $i | cut -d ':' -f 3`
echo "hello,$user your uid is $uid"
done
a=`wc -l $File`
d=`echo $a |cut -d ' ' -f 1`
echo "There are $d uesrs"
#!/bin/bash
File="/etc/passwd"
for i in `cat $File`;
do
user=`echo $i | cut -d ':' -f 1`
uid=`echo $i | cut -d ':' -f 3`
echo "hello,$user your uid is $uid"
done
a=`wc -l $File`
d=`echo $a |cut -d ' ' -f 1`
echo "There are $d uesrs"
0
#! /bin/bash
cat /etc/passwd |awk '{print $1 $3}' > 1.txt
x=`wc -l 1.txt`
i=1
if [ $i -le $x ];then
a=`sed '$i'p 1.txt |cut -f1`
b=`sed '$i'p 1.txt |cut -f2`
echo "HELLO,$a,your uid is $b "
i=$[$i+1]
fi
echo "this scripts total $x users"
cat /etc/passwd |awk '{print $1 $3}' > 1.txt
x=`wc -l 1.txt`
i=1
if [ $i -le $x ];then
a=`sed '$i'p 1.txt |cut -f1`
b=`sed '$i'p 1.txt |cut -f2`
echo "HELLO,$a,your uid is $b "
i=$[$i+1]
fi
echo "this scripts total $x users"
0
#!/bin/bash
user_num=`cat /etc/passwd |awk -F':' '{print $1}' |wc -l`
for i in `seq 1 $user_num`
do
user=`cat /etc/passwd |head -$i |tail -1 |awk -F':' '{print $1}'`
uid=`cat /etc/passwd |head -$i |tail -1 |awk -F':' '{print $3}'`
echo "HELLO,$user,your uid is $uid"
done
echo "There are $user_num users."
user_num=`cat /etc/passwd |awk -F':' '{print $1}' |wc -l`
for i in `seq 1 $user_num`
do
user=`cat /etc/passwd |head -$i |tail -1 |awk -F':' '{print $1}'`
uid=`cat /etc/passwd |head -$i |tail -1 |awk -F':' '{print $3}'`
echo "HELLO,$user,your uid is $uid"
done
echo "There are $user_num users."
编辑回复