gpt4 book ai didi

linux - 修改 shell 脚本以返回与正确用户 ID 关联的名称

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:34 24 4
gpt4 key购买 nike

我有一个 shell 脚本,需要对其进行修改以返回具有关联用户 ID 的全名。我真的可以使用 so 帮助详细解释如何使用正确的语法正确修改此 shell 脚本。如果有人可以帮助我了解如何完成这项任务,我将非常感谢您提供的任何帮助。我是来这里学习的,我对这个社区还很陌生,所以如果发布的内容不正确或格式错误,请告诉我我做错了什么。提前致谢

这里是期望的输出:

$ ./findName.sh eiei

SETSUNA F SEIEI

$

下面是 shell 脚本本身。文件/acct/commmon/spring-names 是所有用户 ID 和全名所在的位置。

#!/bin/sh
# findName.sh


if [ $# -eq 1 ]; then
# if there is exactly one command line arg used with the command
# do something(s).
echo "Your command line entry is: $1" > test
if [ -s ./test ]; then
cat test
else
echo "You should not see this line as output....."
fi
else

# tell the user how to use the command and exit the script
echo "usage: `basename $0` [only_one_argument]"
exit 1

最佳答案

我不确定我是否完全理解您要执行的操作,但我假设您想要使用 $1 arg 运行脚本 ./findName.sh。然后该脚本将花费 $1 并在文件中搜索名称?如果我错了请纠正我,我会更新我的答案。我做了 shebang 行 bash,因为你有 bash 标记。

原创:

#!/bin/sh
# findName.sh


if [ $# -eq 1 ]; then
# if there is exactly one command line arg used with the command
# do something(s).
echo "Your command line entry is: $1" > test
if [ -s ./test ]; then
cat test
else
echo "You should not see this line as output....."
fi
else

# tell the user how to use the command and exit the script
echo "usage: `basename $0` [only_one_argument]"
exit 1

修订:

#!/bin/bash
# findName.sh
searchFile="/acct/commmon/spring-names"

if [[ $1 = "" ]] ; then
echo "You need to supply an argument to the script. Please rerun the script. syntax is below."
echo "./`basename $0` john"
exit 2
fi

grep -i $1 ${searchFile}
if [[ $? = "1" ]] ; then
echo "$1 was not found in ${searchFile}"
fi

退出

关于linux - 修改 shell 脚本以返回与正确用户 ID 关联的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297803/

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