我的 shell 脚本和 shellcheck.net 有问题。下面的脚本就像一个魅力,但 shellcheck 告诉我必须引用变量,但在脚本中引用变量会阻止脚本工作(我需要分词)。
有人能帮助我吗?
apt-get $COMMAND -y -qq $PACKAGES >/dev/null &
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
Run Code Online (Sandbox Code Playgroud) 我想创建 ubuntu 用户并自动设置密码。我想要脚本运行命令:
useradd -s /usr/sbin/nologin username
passwd username
并设置密码1234
请帮我
我正在学习 Bash,但有些东西在我的书中没有解释。首先我会发布一个脚本,然后我会通过脚本提出问题。
Bash 脚本:
$ cat sortmerg
#!/bin/bash
usage ()
{
if [ $# -ne 2 ]; then
echo "Usage: $0 file1 file2" 2>&1
exit 1
fi
}
# Default temporary directory
: ${TEMPDIR:=/tmp}
# Check argument count
usage "$@"
# Set up temporary files for sorting
file1=$TEMPDIR/$$.file1
file2=$TEMPDIR/$$.file2
# Sort
sort $1 > $file1
sort $2 > $file2
# Open $file1 and $file2 for reading. Use file descriptors 3 and 4.
exec 3<$file1
exec 4<$file2
# Read the first …Run Code Online (Sandbox Code Playgroud) 我尝试创建一个 .sh 文件来检查服务器是否正在运行以及是否启动它。我没有从这个脚本中得到任何回声或任何失败,但它没有运行。有人可以帮我解决这个问题或更好的方法来纠正它。我不是程序员,我只是编辑了一些其他脚本来获得它。
JAVA="java -Xms512M -Xmx3072M -XX:PermSize=128m -jar FTBServer.jar nogui"
DIR="/home/jon/FTB/FTBnew"
if [ "$1" = start ] ; then
if ps -ef | grep FTBServer.jar | grep -v -q grep ; then
echo "Minecraft is already running"
else
echo "Starting!" && cd $DIR && screen -dmS minecraft $JAVA
sleep 7
if ps -ef | grep minecraft_server.jar | grep -v -q grep ; then
echo "Minecraft server started successfully"
else
echo "Minecraft server failed to start"
fi
fi
fi
Run Code Online (Sandbox Code Playgroud)
这就是我得到的,当我运行它时
root@FTB:/home/jon/FTB/FTBnew# …Run Code Online (Sandbox Code Playgroud) 我找到了以下我想运行的 bash 脚本,但我不完全确定它要做什么。有人可以解释以下 bash 脚本的工作原理吗?即它在做什么以及它是如何做到的?
echo - "Who are you looking for: "
read user
if [ -n "$user" ]
then
list=`w | grep $user | cut -c19-30`
if [ "$list" != "" ]
then
echo "The user $user is logged in from $list"
else
echo "The user $user is not logged in now"
fi
fi
Run Code Online (Sandbox Code Playgroud) 我有两个用户 User1 和 User2。
我在 User2 主文件夹中有这个 script1.sh:
#!/bin/bash
cd /home/user1 && ./script2.sh
Run Code Online (Sandbox Code Playgroud)
调用script2.sh:
#!/bin/bash
if [ 2 > 1 ]; then
echo TRUE!.
else
echo NOT TRUE!
fi
Run Code Online (Sandbox Code Playgroud)
现在,当我使用 user1 执行 script1 时,一切正常。当我使用 user2 执行时,出现此错误:
./script2.sh: line 3: 1: Permission denied
Run Code Online (Sandbox Code Playgroud)
说我不能……如果?如果我在终端上写下所有内容,它也可以工作。所以我的权限:
-rwxrwxr-x 1 user2 group_with_both_users 149 Ago 19 02:41 script2.sh
-rwxr-xr-x 1 user2 group_with_both_users 60 Ago 19 01:38 script1.sh
Run Code Online (Sandbox Code Playgroud)
我真的不知道发生了什么。
我已经编写了这个 bash 脚本,它旨在检查您的输入以查看它是否是y,n或其他内容(这将是更大项目的一部分):
#!/bin/bash
echo "Have you updated the PATH variable in your .bashrc file yet? [y/n]"
read response
if [$response = "y"]; then
echo "Checkpoint passed"
set $checkpoint = "t"
elif [$response = "n"]; then
echo "Please set the PATH variable in your .bashrc file before running this script."
set $checkpoint = "f"
else
echo "Please only use 'y' and 'n'."
set $checkpoint = "f"
fi
Run Code Online (Sandbox Code Playgroud)
但是每次运行它时,无论我输入什么,都会收到与此类似的错误:
Have you updated the PATH variable in your …Run Code Online (Sandbox Code Playgroud) 我们应该如何将正在执行的脚本从一个文件夹复制到另一个文件夹?
脚本内容为:
#!/bin/sh
zenity --forms --title="Add Friend" \
--text="Enter information about your friend." \
--separator="," \
--add-entry="First Name" \
--add-entry="Family Name" \
--add-entry="Email" \
--add-calendar="Birthday" >> addr.csv
case $? in
0)
echo "Friend added.";;
1)
echo "No friend added."
;;
-1)
echo "An unexpected error has occurred."
;;
esac
Run Code Online (Sandbox Code Playgroud)
假设我已将上述脚本保存为test.shin home/user/Documents/sh/,当我通过双击执行test.sh 时,它应该将自身复制到meow文件夹中home/user/wow/meow/。
我有一个登录到我的 Beaglebone 的期望脚本:
#!/usr/bin/expect -f
spawn ssh debian@192.168.7.2
expect "debian@192.168.7.2's password:"
send "temppwd\r"
interact &&
mkdir emma &&
cd emma
Run Code Online (Sandbox Code Playgroud)
这有效并且它登录到 debian 帐户。但是,它会在interact其他两个命令未执行后停止。我该怎么做才能做到这一点?
编辑
好的,谢谢 andy256 我想这interact是错误的,但是,我明白了
执行时无效的命令名称“mkdir”
如何将期望脚本与普通 shell 脚本结合起来?
提前致谢 !
awk -F'[ ()+:]+' '
Run Code Online (Sandbox Code Playgroud)
我在我的脚本中使用它。在这种情况下,我可以花时间在括号中,例如:
Fri Dec 18 11:13 - 11:15 (00:02)
Fri Dec 18 09:11 - 19:42 (1+10:31)
Run Code Online (Sandbox Code Playgroud)
但它是如何工作的?有人可以解释我吗?
scripts ×10
bash ×8
command-line ×5
.sh ×2
20.04 ×1
awk ×1
beagleboard ×1
expect ×1
gedit ×1
users ×1