对于我第一年的计算机科学,我需要了解 Shell 脚本,但我在这方面真的很差。例如,我写了这个脚本
#!/bin/bash
[ $(whoami) != "root" ] && echo "Run as superuser" && exit 1
while getopts "ugb: " opt; do
case $opt in
u)
read -p "Wat is de naam van de user die je wilt toevoegen?: " username
[ $username == ""] && echo "Geen usename meegegeven!" && exit 1
read -p "Wat is de groepsnaam voor de gebruiker?: " group_name
[ "$(getent group $group_name)" == "" ] && echo "De groep bestaat niet, exiting" \ && exit 1
echo "adduser -m -g $group_name $username"
create_softlink $group_name $username
g)
read -p "Wat is de groepnaam voor de user?: " group_name
[ "$(getent group $group_name)" != "" ] && echo "De groep bestaat, exiting" \ && exit 1
addgroup "$groupname"
mk_shared_folder "$groupname"
b)
BACKUP_FOLDER="/var/backup"
[ ! -d "$BACKUP_FOLDER"] && mkdir -p "$BACKUP_FOLDER"
[ -d "$SHARED_FOLDER/$1"] && echo "Shared folder bestaat niet" && exit 1
zip "$SHARED_FOLDER/$1" "$BACKUP_FOLDER/$1-$(date +"%m-%d-%y")"
现在我想执行这个脚本。例如创建一个用户。在一个简单的脚本中,你只需执行./scriptname.sh
。但我不知道命令是什么。
如果这完全没有意义,我很抱歉。我在这方面真的很差,几乎什么都不懂。
编辑:我的问题已被标记为重复。但是 ./scriptname.sh 不会运行该脚本。
答案1
要运行脚本,您需要执行以下操作之一。假设您的脚本存储在/home/maarten/script.sh
:
将其
bash
作为参数:bash /home/maarten/script.sh
使其可执行并通过在命令行上写入其路径来运行它:
chmod +x /home/maarten/script.sh /home/maarten/script.sh
使其可执行,并将其移动到您的
$PATH
:chmod +x /home/maarten/script.sh mkdir -p ~/bin && mv /home/maarten/script.sh ~/bin
如果
bin
您的主目录中存在一个目录(~/bin
即$HOME/bin
),它将在$PATH
您下次登录时自动添加到您的目录中。要为当前会话手动添加它而不注销然后再次登录,您可以运行source ~/.profile
或PATH="$PATH:~/bin
。
现在,您的脚本也有一些语法错误。 之后[
和之前都需要空格]
,while
必须以 结尾do
,case
必须以 结尾esac
,并且每个 case 语句都以结尾;;
,并且您应该始终引用你的变量:
#!/bin/bash
[ $(whoami) != "root" ] && echo "Run as superuser" && exit 1
while getopts "ugb: " opt; do
case $opt in
u)
read -p "Wat is de naam van de user die je wilt toevoegen?: " username
[ "$username" == "" ] && echo "Geen usename meegegeven!" && exit 1
read -p "Wat is de groepsnaam voor de gebruiker?: " group_name
[ "$(getent group $group_name)" == "" ] && echo "De groep bestaat niet, exiting" && exit 1
## I removed the echo since you probably want to run the command
adduser -m -g "$group_name" "$username"
## I assume your real script has a function called
## "create_softlink", right? This won't work otherwise
create_softlink "$group_name" "$username"
;;
g)
read -p "Wat is de groepnaam voor de user?: " group_name
[ "$(getent group $group_name)" != "" ] && echo "De groep bestaat, exiting" \ && exit 1
addgroup "$groupname"
## Like "create_softlink" above, I am assuming you have a
## function called "mk_shared_folder" somewhere.
mk_shared_folder "$groupname"
;;
b)
BACKUP_FOLDER="/var/backup"
[ ! -d "$BACKUP_FOLDER" ] && mkdir -p "$BACKUP_FOLDER"
## Note that $1 is the 1st parameter passed to the script.
## However, since you're using getopts, and b expects an argument
## that argument is available as $OPTARG when the current $opt is b
## I have changed the script accordingly.
[ -d "$SHARED_FOLDER/$2" ] && echo "Shared folder bestaat niet" && exit 1
zip "$SHARED_FOLDER/$OPTARG" "$BACKUP_FOLDER/$OPTARG-$(date +"%m-%d-%y")"
;;
esac
done