假设我们有一个这样的脚本。
#!/bin/bash
set -e
ls notexist && ls notexist
echo still here
由于设置了-e而无法退出
但
#!/bin/bash
set -e
ls notexist || ls notexist
echo still here
将要。为什么?
命令
ls /etc/pve/lxc/*([0-9]).conf
在控制台
中工作
结果:文件找到了!
/etc/pve/lxc/107.conf
但不在脚本
中
#!/bin/bash
结果:错误
syntax error near unexpected token `('
ls /etc/pve/lxc/*([0-9]).conf
,然后我屏蔽了脚本
中的括号
ls /etc/pve/lxc/*\([0-9]\).conf
结果:没有找到文件
ls: cannot access '/etc/pve/lxc/*([0-9]).conf': No such file o
我已经写了一个简单的shell脚本来做一些自动化工作。基本上,脚本会搜索当前路径中的所有文件,如果文件是指定的文件,它会执行一些操作。以下是相关的行
#!/bin/bash
for i in `ls *`
do
if [$i =="ls.sh"]
then .... //do something
fi
done
但是,第3行中的字符串比较不起作用,当我运行脚本时会得到以下结果--
./ls.sh: line 3: [scripth.sh: command not found
./ls.sh: line 3: [scripth.sh~: command not found
./
要么对我来说太晚了,要么我在这里错过了一些天真的东西。
这是一个人为设计的例子
#!/bin/bash
command="ls -al > check.txt"
$command
当我在shell上运行这个脚本时,我猜是由于">“运算符导致的错误。无论如何,我可以从shell脚本内部重定向输出。我认为这很简单:
ls -la > temp.txt
ls: cannot access >: No such file or directory
ls: cannot access temp.txt: No such file or directory
在编写了一个非常简单的python脚本给ls之后,我的项目目录的内容作为一个测试:
from subprocess import *
p = Popen(['ls /Users/Nelson/Projects'], stdout=PIPE, shell=True)
print(p.communicate()[0].decode())
它将错误排除在外:
/bin/bash: ls: command not found
我做了一些测试,发现了基本命令pwd、echo和cd --所有的单词都很好,但ls没有。我尝试使用executable='/bin/bash'选项
我偶然发现了一种我无法解释的奇怪行为。我试着缩小问题的范围。我有以下测试testscript.sh脚本: echo before
adb shell ls
echo after 如果我使用bash -x testscript.sh运行该脚本,一切都将按预期运行,并得到以下输出: + echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
+ echo before
before 但是,如果我使用cat testscript.sh | bash -
可以从交互式tclsh会话中使用Bash命令。例如,在tclsh会话中,您可以使用
% ls
而不是
$ exec ls
然而,你不能有一个直接调用bash命令的tcl脚本(也就是没有exec)。
如何让tclsh在解释tcl脚本文件时识别bash命令,就像在交互式会话中一样?
我猜有一些tcl包(或类似的东西),它在启动交互会话时自动加载,以支持对bash命令的直接调用。如何在tcl脚本文件中手动加载?
我正在我的bash脚本(比如child.sh)中调用bash脚本(比如parent.sh),我希望脚本(child.sh)中的错误被困在我的父脚本(parent.sh)中。
我通过和阅读。基于此,我认为我应该对我的父脚本执行set -E,以便由子shell继承这些陷阱。因此,我的代码如下
parent.sh
#!/bin/bash
set -E
error() {
echo -e "$0: \e[0;33mERROR: The Zero Touch Provisioning script failed while running the command $BASH_COMM
假设我有以下bash脚本:
#/bin/sh
#next line will work correctly, outputting user name and current directory
start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'
MYVAR="start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'"
#next line will fail with following err
我有一个包含以下文本的文件"FileList.txt“:
/home/myusername/file1.txt
~/file2.txt
${HOME}/file3.txt
所有3个文件都存在于我的主目录中。我希望处理来自bash脚本的列表中的每个文件。下面是一个简化的示例:
LIST=`cat FileList.txt`
for file in $LIST
do
echo $file
ls $file
done
当我运行脚本时,我得到以下输出:
/home/myusername/file1.txt
/home/myusername/file1.txt
~/file2.txt
l