gpt4 book ai didi

linux - bash 文件 Linux Ubuntu

转载 作者:太空宇宙 更新时间:2023-11-04 05:38:15 27 4
gpt4 key购买 nike

我不知道为什么我的 bash 文件在 Linux Ubuntu 中不起作用。

#!/bin/bash`   
echo -n "Write user name : "
read NAME
cd
if grep -c "$NAME" /etc/passwd -eq 0; then
echo "$NAME doesn't esist"
else
echo "$NAME already esist"
fi`

它给出了错误:

$ bash ./name.sh 
Write user name : root
grep: root: No such file or directory
/etc/passwd:3
grep: 0: No such file or directory

最佳答案

代码没有在子 shell 中执行 grep,并且 if 条件需要用方括号括起来。

#!/bin/bash

echo -n "Write user name : "
read NAME
cd
if [ `grep -c "$NAME" /etc/passwd` -eq 0 ]; then
echo "$NAME doesn't esist"
else
echo "$NAME" already esist
fi

输出:

$ bash ./name.sh
Write user name : root
root already esist
$ bash ./name.sh
Write user name : ddd
ddd doesn't esist

关于linux - bash 文件 Linux Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com