我想代表这样的多个条件:
if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]
then
echo abc;
else
echo efg;
fi
Run Code Online (Sandbox Code Playgroud)
但是当我执行脚本时,它会显示出来
syntax error at line 15: `[' unexpected,
Run Code Online (Sandbox Code Playgroud)
第15行显示是否....
这种情况怎么了?我猜这有点不对劲().
有人能告诉我是否应该在shell脚本中包含变量的引号?
例如,以下是正确的:
xdg-open $URL
[ $? -eq 2 ]
Run Code Online (Sandbox Code Playgroud)
要么
xdg-open "$URL"
[ "$?" -eq "2" ]
Run Code Online (Sandbox Code Playgroud)
如果是这样,为什么?
我希望在Bash if语句中包含一些条件组.具体来说,我正在寻找以下内容:
if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
Run Code Online (Sandbox Code Playgroud)
我如何按照我在Bash中使用if语句描述的方式将条件组合在一起?我感谢你对此有任何想法.
你会怎样用bash来判断某个目录中是否存在特定扩展名的文件?
就像是
if [ -e *.flac ]; then
echo true;
fi
Run Code Online (Sandbox Code Playgroud) 我想检查两个变量是否都已设置或均未设置。我尝试了多种选择,这是我想出的最干净的解决方案:
if [ ! -z $A -a -z $B ] || [ -z $A -a ! -z $B ]; then
#error
fi
#success
Run Code Online (Sandbox Code Playgroud)
当我运行 A 和 B 设置的脚本时 - 它运行良好。但是当我在缺少 A 的情况下运行它时,我得到:
./test.sh: line 3: [: too many arguments
./test.sh: line 3: [: too many arguments
Run Code Online (Sandbox Code Playgroud)
第 3 行是条件语句。
当我在 B 缺失的情况下运行它时,我得到:
./test.sh: line 3: [: argument expected
./test.sh: line 3: [: argument expected
Run Code Online (Sandbox Code Playgroud)
是我的情况语法错误还是我错过了其他东西?
GitHub上的SSH配置似乎是一场噩梦.我有多个GitHub帐户,但我可以拥有多个SSH密钥.在GitHub SSH配置部分,他们提到了这一点:
============
ssh-keygen -t rsa -C"your_email@example.com"使用提供的电子邮件作为标签创建新的ssh密钥
生成公钥/私钥rsa密钥对.
我们强烈建议保留默认设置,因此当系统提示您"输入要保存密钥的文件"时,只需按Enter继续.
#输入保存密钥的文件(/Users/you/.ssh/id_rsa):[按enter键]
为什么我总是要使用id_rsa文件?它将覆盖我现有的密钥.无论如何,我在这里给出一个新名称并生成密钥.我执行将其添加到代理程序的所有其他步骤,在GitHub SSH密钥部分中进行更新.
完成所有这些步骤后,我进入最后一步,即:
ssh -vT git@github.com
Hi animesh11! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3128, received 1976 bytes, in 0.5 seconds
Bytes per second: sent 6077.0, received 3838.9
debug1: Exit status 1
Run Code Online (Sandbox Code Playgroud)
一切都是笨拙的海鲂,但不知何故git clone仍然抱怨:
MacBook-Pro:Documents animeshsaxena$ git clone git@github.com:regentmarkets/devbox.git
Cloning into 'devbox'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please …Run Code Online (Sandbox Code Playgroud) 我对 Bash 脚本相当陌生,目前正在根据用户的操作系统创建一个安装程序来使用 Docker-Compose 安装 Docker 客户端。该脚本不打算在每个操作系统上运行,其范围仅适用于 Ubuntu 16.04、18.04、CentOS 7 和 8。
我编写了以下代码来验证用户的操作系统并开始安装过程(目前,我正在尝试使其适用于 Ubuntu 18.04):
################
### Check OS ###
################
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
RESULT="$OS $VER"
printf -v $RESULT "Ubuntu 18.04"
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的脚本来检查一些存储库更新,如果需要,我正在从这些更新中创建新的软件包以安装它所引用的那些程序的新版本(在Arch Linux中).所以我在执行真实脚本之前做了一些测试.
问题是我从这段代码中得到错误[: excessive number of arguments(但我认为正确的翻译[: too many arguments):
# Won't work despite the double quoted $r
if [ "$r" == *"irt"* ]; then
echo "TEST"
fi
Run Code Online (Sandbox Code Playgroud)
代码是通过添加双方括号来修复的,我感谢@ user568458的这个SO答案:
# Makes the code works
if [[ "$r" == *"irt"* ]]; then
echo "TEST"
fi
Run Code Online (Sandbox Code Playgroud)
注意,$r定义如下:
# Double quotes should fix it, right? Those special characters/multi-lines
r="$(ls)"
Run Code Online (Sandbox Code Playgroud)
还要注意,一切都在循环内,循环成功.每次if比较匹配时都会出现问题,而不是打印已"TEST"发出的,直接跳到循环的下一次迭代(没问题:此后没有代码存在if).
我的问题是:为什么每次字符串匹配时都会发生错误?根据我的理解,双引号就足以解决它.此外,如果我依靠双方括号来修复它,一些shell将无法识别它(参考上面提到的答案).有什么选择?
Shell脚本似乎是一个全新的编程范例.我从来没有完全掌握细节,也无法找到一个很好的源代码.