我如何在 Korn Shell 中写这个?
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该脚本与编写的内容完全兼容 Kornshell。你不必对它做任何事。
Kornshell 和 Bash 确实有所不同,但只有很少的地方。最常见的是:
print
而 Bash 没有。然而两者都有printf
。排版
的工作方式。 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:
print
and Bash doesn't. However both haveprintf
.typeset
works. Kornshell has a much richer syntax. Bash uses other commands to do the same thing.set -o
to set options, but Bash also has theshopt
settings. And, Bash has better prompt syntax. You won't believe what I have to go through to set my Kornshell prompt to do whatPS="\u@\h:\w$ "
does in Bash.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.
我建议将
[[
...]]
替换为[
...]
并使用- eq
/-ne
使脚本在不同的 shell 上更加可移植。I suggest replacing
[[
...]]
by[
...]
and using-eq
/-ne
to make the script more portable on different shells.