我如何在 Korn Shell 中写这个?

发布于 2024-12-27 15:40:20 字数 841 浏览 0 评论 0原文

如何在 Korn Shell 中重写此脚本?这是在 Bash 中吧?我对所有 shell 之间的实际差异有点困惑......我将其转换为 Korn Shell 的方向正确吗?

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

# Search for user
fullname=$(cut -f1 -d: /etc/passwd | grep "$1")
if [[ $? -eq 0 ]]; then
                echo "User already found:"
                grep $1 /etc/passwd
        exit
        else
                #get numbers
                cat /etc/passwd | gawk -F: '{print $3}' | sort -n > currentuid4
                #get last number
                last=`tail -1 currentuid4`
                echo last $last
                #add +1
                newuid=`expr $last + 1`
                #print it
                echo "ADDED: $1 with UID: $newuid"
        exit
fi

How do I rewrite this script in Korn Shell? It's in Bash right? I'm a little confused to the actual differences between all the shells... Am I on the right track to converting it to Korn Shell?

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

# Search for user
fullname=$(cut -f1 -d: /etc/passwd | grep "$1")
if [[ $? -eq 0 ]]; then
                echo "User already found:"
                grep $1 /etc/passwd
        exit
        else
                #get numbers
                cat /etc/passwd | gawk -F: '{print $3}' | sort -n > currentuid4
                #get last number
                last=`tail -1 currentuid4`
                echo last $last
                #add +1
                newuid=`expr $last + 1`
                #print it
                echo "ADDED: $1 with UID: $newuid"
        exit
fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2025-01-03 15:40:20

该脚本与编写的内容完全兼容 Kornshell。你不必对它做任何事。

Kornshell 和 Bash 确实有所不同,但只有很少的地方。最常见的是:

  • Kornshell 有 print 而 Bash 没有。然而两者都有printf
  • Kornshell 和 Bash 的不同之处在于排版 的工作方式。 Kornshell 具有更丰富的语法。 Bash 使用其他命令来做同样的事情。
  • Bash 具有更丰富的命令行功能。 Kornshell 和 Bash 都有 set -o 来设置选项,但 Bash 也有 shopt 设置。而且,Bash 具有更好的提示语法。您不会相信我必须经历什么才能设置我的 Kornshell 提示符以执行 PS="\u@\h:\w$ " 在 Bash 中执行的操作。
  • 我相信算术处理也存在一些差异。我只是无法立即想到它。

顺便说一句,这个脚本不会将用户添加到 /etc/passwd 文件中,正如您为它提供新用户时所声明的那样。

This script is completely Kornshell compatible as written. You don't have to do a thing to it.

Kornshell and Bash do differ, but in very few places. The most common ones are:

  • Kornshells have print and Bash doesn't. However both have printf.
  • Kornshell and Bash differ in the way typeset works. Kornshell has a much richer syntax. Bash uses other commands to do the same thing.
  • Bash has a richer set of command line features. Kornshell and Bash both have set -o to set options, but Bash also has the shopt settings. And, Bash has better prompt syntax. You won't believe what I have to go through to set my Kornshell prompt to do what PS="\u@\h:\w$ " does in Bash.
  • I believe there's some differences in arithmetic handling too. I just can't think of it right off the top of my head.

This script, by the way, doesn't add a user to the /etc/passwd file as it claims when you give it a new user.

恋竹姑娘 2025-01-03 15:40:20

我建议将 [[ ... ]] 替换为 [ ... ] 并使用 - eq/-ne 使脚本在不同的 shell 上更加可移植。

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

# Search for user                                                                                                                                              
fullname=$(cut -f1 -d: /etc/passwd | grep "$1")
if [ "$?" -eq 0 ]; then
                echo "User already found:"
                grep $1 /etc/passwd
        exit
        else
                #get numbers                                                                                                                                   
                cat /etc/passwd | gawk -F: '{print $3}' | sort -n > currentuid4
                #get last number                                                                                                                               
                last=`tail -1 currentuid4`
                echo last $last
                #add +1                                                                                                                                        
                newuid=`expr $last + 1`
                #print it                                                                                                                                      
                echo "ADDED: $1 with UID: $newuid"
        exit
fi

I suggest replacing [[ ... ]] by [ ... ] and using -eq/-ne to make the script more portable on different shells.

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

# Search for user                                                                                                                                              
fullname=$(cut -f1 -d: /etc/passwd | grep "$1")
if [ "$?" -eq 0 ]; then
                echo "User already found:"
                grep $1 /etc/passwd
        exit
        else
                #get numbers                                                                                                                                   
                cat /etc/passwd | gawk -F: '{print $3}' | sort -n > currentuid4
                #get last number                                                                                                                               
                last=`tail -1 currentuid4`
                echo last $last
                #add +1                                                                                                                                        
                newuid=`expr $last + 1`
                #print it                                                                                                                                      
                echo "ADDED: $1 with UID: $newuid"
        exit
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文