Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用具有精确文件格式的shell脚本查找单词并将其添加到特定行_Shell - Fatal编程技术网

使用具有精确文件格式的shell脚本查找单词并将其添加到特定行

使用具有精确文件格式的shell脚本查找单词并将其添加到特定行,shell,Shell,我的问题是在文件中添加用户名,我真的坚持要继续,请帮助。 问题:我有一个名为usrgrp.dat的文件。此文件的格式如下所示: ADMIN:srikanth,admin DEV:dev1 TEST:test1 我正在尝试编写一个shell脚本,该脚本将为我提供如下输出: Enter group name: DEV Enter the username: dev2 我的预期产出是: User added to Group DEV 如果我看到usrgrp.dat的内容,它现在应该如

我的问题是在文件中添加用户名,我真的坚持要继续,请帮助。
问题:我有一个名为
usrgrp.dat
的文件。此文件的格式如下所示:

ADMIN:srikanth,admin  
DEV:dev1  
TEST:test1
我正在尝试编写一个shell脚本,该脚本将为我提供如下输出:

Enter group name: DEV  
Enter the username: dev2
我的预期产出是:

User added to Group DEV
如果我看到
usrgrp.dat
的内容,它现在应该如下所示:

DEV:dev1,dev2
TEST:test1
如果我试图在该组中添加已存在的用户,则会出现这样的错误:用户已存在。我正在使用以下脚本进行尝试:

#!/bin/sh
dispgrp()
{
        groupf="/home/srikanth/scm/auths/group.dat"
        for gname in `cat $groupf | cut -f1 -d:`
        do
                echo $gname
        done
        echo "Enter the group name:"
        read grname
        for gname in `cat $groupf | cut -f1 -d:`
        do
                if [ "$grname" = "$gname" ]
                then
                        echo "Enter the username to be added"
                        read uname
                        for grname in `cat $groupf`
                        do

                                $gname="$gname:$uname"
                                exit 1
                        done
                fi
        done
}
echo "Group display"
dispgrp
我陷入困境,需要您的宝贵帮助。

类似(假设您的shell是bash):

adduser(){
本地grp=“$1”
本地用户=“2美元”
本地gfile=“$3”
如果!grep-q“^$grp:$gfile”,则
echo“没有这样的组:$grp”
返回1
fi
如果grep-q“^$grp:.\\\\$gfile”,则
echo“用户$User已在组$grp中”
其他的
sed-i“/^$grp:/s/\$/,$user/“$gfile”
echo“用户$User已添加到组$grp”
fi
}
读取-p“输入组名:”grp
read-p“输入要添加的用户名:”user
adduser“$grp”“$user”/home/srikanth/scm/auths/group.dat
#/垃圾箱/垃圾箱
dispgrp()
{
groupf=“/home/srikanth/scm/auths/group.dat”
tmpfile=“/path/to/tmpfile”
#如果列表可能很长,您可能希望通过管道或多或少地发送此消息
类别“$groupf”|切割-f1-d:
echo“输入组名:”
读取grname
如果grep“$grname”“$groupf”>/dev/null 2>&1
然后
echo“输入要添加的用户名”
读uname
如果!grep“^$grname:.\”“$groupf”>/dev/null 2>&1
然后
sed“/^$grname:/s/\$/,$uname/“$groupf”>“$tmpfile”和&mv“$tmpfile”$groupf”
其他的
echo“用户$uname已存在于组$grname中”
返回1
fi
其他的
回显“未找到组”
返回1
fi
}
回显“组显示”
dispgrp
当循环为您完成时,您不需要使用循环(例如
cat
sed
grep

不要使用
for
来迭代
cat
的输出

不要使用
exit
从函数返回。使用
返回

非零退出或返回代码表示错误或失败。使用
0
进行正常、成功的返回。如果不指定,则这是隐式操作

学习使用
sed
grep


既然你的shebang说的是
#/bin/sh
,我上面所做的更改是基于Bourne shell并假设POSIX实用程序(不是GNU版本)。

感谢您的宝贵建议,我已经尝试了您和glenn的解决方案。当我按照您的建议尝试执行脚本时,它在接受用户名后挂起。@Srikanth:对不起,我有几个错误现在已修复。感谢您的宝贵帮助。它修好了。
adduser() {
    local grp="$1"
    local user="$2"
    local gfile="$3"

    if ! grep -q "^$grp:" "$gfile"; then
        echo "no such group: $grp"
        return 1
    fi

    if grep -q "^$grp:.*\\<$user\\>" "$gfile"; then
        echo "User $user already in group $grp"
    else
        sed -i "/^$grp:/s/\$/,$user/" "$gfile"
        echo "User $user added to group $grp"
    fi 
}

read -p "Enter the group name: " grp
read -p "Enter the username to be added: " user
adduser "$grp" "$user" /home/srikanth/scm/auths/group.dat
#!/bin/sh
dispgrp()
{
        groupf="/home/srikanth/scm/auths/group.dat"
        tmpfile="/path/to/tmpfile"
        # you may want to pipe this to more or less if the list may be long
        cat "$groupf" | cut -f1 -d:
        echo "Enter the group name:"
        read grname
        if grep "$grname" "$groupf" >/dev/null 2>&1
        then
            echo "Enter the username to be added"
            read uname
            if ! grep "^$grname:.*\<$uname\>" "$groupf" >/dev/null 2>&1
            then
                sed "/^$grname:/s/\$/,$uname/" "$groupf" > "$tmpfile" && mv "$tmpfile" "$groupf"
            else
                echo "User $uname already exists in group $grname"
                return 1
            fi
        else
            echo "Group not found"
            return 1
        fi
}
echo "Group display"
dispgrp