当我编写 Bash 代码时,例如复制文件,如果文件不存在,在终端中我会看到类似于file not found的错误。如果运行脚本的用户没有必要的权限,错误类似于权限被拒绝。
基本上,独立于用于编写代码的编程语言,复制文件的代码将要求操作系统(在我的情况下为 Linux)这样做。如果出现问题,操作系统将返回相应的错误(编号和消息)。
有没有我可以运行的命令来列出所有标准错误代码?
my_test:
ifdef $(toto)
@echo 'toto is defined'
else
@echo 'no toto around'
endif
Run Code Online (Sandbox Code Playgroud)
$ make my_test
no toto around
$ make my_test toto
toto is defined
Run Code Online (Sandbox Code Playgroud)
$ make my_test
no toto around
$ make my_test toto
no toto around
make: *** No rule to make target `toto'. Stop.
Run Code Online (Sandbox Code Playgroud)
当我运行时,make my_test我按预期得到 else 文本no toto around。然而
make my_test toto
no toto around
make: *** No rule to make target `toto'. Stop.
Run Code Online (Sandbox Code Playgroud)
生成文件版本
$ make -v …Run Code Online (Sandbox Code Playgroud) 我想输出hello world超过 20 个字符。
printf "%-20s :\n\n" 'hello world!!'
# Actual output
hello world!! :
# Wanted output
hello world!!========:
Run Code Online (Sandbox Code Playgroud)
但是,我不想用空格来完成,而是用“ = ”来代替。我怎么做?
func() {
echo 'hello'
echo 'This is an error' >&2
}
a=$(func)
b=???
Run Code Online (Sandbox Code Playgroud)
我想将 stderr 重定向到b变量而不创建临时文件。
echo $b
# output should be: "This is an error"
Run Code Online (Sandbox Code Playgroud)
有效但使用临时文件的解决方案:
touch temp.txt
exec 3< temp.txt
a=$(func 2> temp.txt);
cat <&3
rm temp.txt
Run Code Online (Sandbox Code Playgroud)
所以问题是,如何在不需要临时文件的情况stderr下将bash函数的重定向func到变量b?
我在vim编辑器中有两行,如下所示
3 àáâ
4 aaa
Run Code Online (Sandbox Code Playgroud)
基于这两行,我想得到下面的结果
'à' => 'a',
'á' => 'a',
'â' => 'a',
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
在我的 makefile 中,我需要根据名为MY_SERVER_ENV.
我试过这个:
gulp:=./node_modules/.bin/gulp
ifeq ($(MY_SERVER_ENV), 'prod')
branch:=production
else
branch:=deploy
endif
checkvariable:
@echo $$branch
Run Code Online (Sandbox Code Playgroud)
我得到的输出:
$ MY_SERVER_ENV=prod 生成检查变量
$
这个想法是将变量设置branch为production如果环境变量MY_SERVER_ENV等于prod,否则设置branch为deploy。
正如你所看到的,echo $$branch什么都不显示
我需要在 bash 循环内执行算术运算,如下所述
CYCLE?=3
COUNT=1
download_when_ready: ## Will try the download operations many times till it succeeds or it reaches 10 tries
while ! composer update $(bundle) 2> /dev/null && [[ $(COUNT) -lt 10 ]]; \
do \
COUNT=$$(( $(COUNT)+1 )); \
SLEEP=$$(( ($(COUNT) / $(CYCLE)) + ($(COUNT) % $(CYCLE)) )); \
echo "count $(COUNT)"; \
echo "cycle $(CYCLE)"; \
echo "sleep $(SLEEP)"; \
sleep $(SLEEP); \
done
Run Code Online (Sandbox Code Playgroud)
这永远不会停止并给出以下内容:
count 0
cycle 4
sleep 0
count 0
cycle 4
sleep …Run Code Online (Sandbox Code Playgroud) 我的bash脚本包含许多mysqldump blabla > dump.sql,mysq balbla < dump.sql以便可以在试运行模式下运行它。
实际上,关键是创建一个功能run来运行我要求它执行的任何操作。
run echo 'hello world'
run mysqldump blabla > dump.sql
run mysql blabla < dump.sql
run ssh blabla
# etc
run() {
if [[ "$(printenv DRY_RUN)" = "yes" ]]
then
echo "${@}"
else
${@}
fi
}
Run Code Online (Sandbox Code Playgroud)
但是,这是行不通的:
run "mysqldump -uuser -ppass dbase > dump.sql"
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
mysqldump:找不到表:">"
bash ×4
make ×3
arithmetic ×1
command-line ×1
linux ×1
parameter ×1
printf ×1
shell ×1
shell-script ×1
stderr ×1
string ×1
variable ×1
vim ×1