【问题标题】:bash script and greping with command linebash 脚本和 greping 命令行
【发布时间】:2023-03-23 01:40:01
【问题描述】:

bash 脚本的新手,所以只是想知道我是否完全正确地编写了这段代码。我试图搜索 /etc/passwd 然后 grep 并打印用户。

usage ()
{
     echo      "usage: ./file.sk user"
}
# test if we have two arguments on the command line
if [ $# != 1 ]
then
    usage
    exit
fi

if [[ $# < 0 ]];then
   usage
   exit
fi

# Search for user
fullname=`grep $1 /etc/passwd | cut -f 5 -d :`
firstname=`grep $1 /etc/passwd | cut -f 5 -d : | cut -f 1 -d " "`

#check if there. if name is founf: print msg and line entry
not sure as how to this or if im doing this right...

我这样做对吗?

【问题讨论】:

  • 你的第二个$# 测试是多余的,如果代码到达它$# 必须是1。
  • 看看finger 命令。
  • 未引用的&lt; 是重定向,而不是数字比较。正如其他人指出的那样,无论如何条件都是多余的,但是这个错误可能很难调试,所以我单独指出。

标签: bash shell command-line grep


【解决方案1】:
grep $1 /etc/passwd |而 IFS=: 读取 -r 用户名 passwd uid gid info home shell 做 回显$用户名:$信息 完毕

【讨论】:

    【解决方案2】:

    这可能对你有用:

    fullname=$(awk -F: '/'$1'/{print $5}' /etc/passwd)
    firstname=${fullname/ *}
    

    【讨论】:

      【解决方案3】:

      你在正确的轨道上。

      但我认为第二个if [[ $# &lt; 0 ]] .... fi 块并没有给你太多。您的第一个测试用例得到了正确的情况,“此脚本需要 1 个参数或退出”。

      另外,我看不出你需要什么名字,所以一个基本的测试是

       case "${fullname:--1}" in 
         -[1] ) printf "No userID found for input=$1\n" ; exit 1 ;;
         * )
           # assume it is OK
           # do what every you want after this case block
         ;;
       esac
      

      如果您确实需要检查,当然可以使用 "${firstname}" 复制此内容。

      如果 ... fi 是,则作为等价物

       if [[ "${fullname}" == "" ]] ; then
          printf "No userID found for input=$1\n" ; exit 1
       fi
      

      注意要更高效,您可以解析 ${fullname} 以获取名字,而无需调用 grep 等,即

       firstname=${fullname%% *}
      

      如果您需要我解释 :--1} 和 %% *} 变量修饰符,请告诉我。

      我希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        而不是这个:

        fullname=`grep $1 /etc/passwd | cut -f 5 -d :`
        firstname=`grep $1 /etc/passwd | cut -f 5 -d : | cut -f 1 -d " "`
        

        试试这个:

        fullname=$(cut -f5 -d: /etc/passwd | grep "$1")
        
        if [[ $? -ne 0 ]]; then
            # not found, do something
        fi
        
        firstname=${fullname%% *} # remove the space and everything after
        

        请注意,我在 grep 之前将答案更改为 cut ,这样如果其他字段与您正在搜索的全名匹配,它就不会得到误报。

        【讨论】:

        • 没关系,我会再次更新我的答案,但同时我做了一些改进,所以还是谢谢!
        【解决方案5】:

        您可以简单地将您的输入读入array,然后打印出您想要的字段,就像这样 -

        grep $1 /etc/passwd | while IFS=: read -a arry; do 
            echo ${arry[0]}:${arry[4]}; 
        done
        

        测试:

        jaypal:~/Temp] echo "root:*:0:0:System Administrator:/var/root:/bin/sh" | 
        while IFS=: read -a arry; do 
            echo ${arry[0]}:${arry[4]}; 
        done
        root:System Administrator
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-09
          • 2017-01-04
          • 2018-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多