问题:
以下shell脚本代码不会产生预期的结果:
# MYSQL, MyUSER MyHost etc ... all defined above as normal
TARG_DB="zztest";
DB_CREATE="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'create database $TARG_DB')";
Run Code Online (Sandbox Code Playgroud)
预期结果:
使用名称创建的新数据库 zztest
实际结果:
使用名称创建的新数据库 $TARG_DB
题:
如何更改此示例代码以进行$TARG_DB插值或扩展,从而产生预期结果?
This contrived bash script demonstrates the issue.
#!/bin/bash
while read -r node ; do
echo checking $node for Agent;
PID=$(ssh $node ""ps -edf | grep [j]ava | awk '{print $2}'"")
echo $PID got to here.
done < ~/agents_master.list
Run Code Online (Sandbox Code Playgroud)
agents_master.list contains 1 server per line:
server1
server2
server3
Run Code Online (Sandbox Code Playgroud)
Which only outputs the following:
checking server1 for Agent
Authorized use only
25176 got to here
Run Code Online (Sandbox Code Playgroud)
Server 2 and 3 aren't even echoed out to screen by the line echo checking $node...
If …
我目前的PS1:
PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]`date +%Y-%m-%d,%H:%M:%S` \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$: '
Run Code Online (Sandbox Code Playgroud)
是的,这是一团糟,但它很好用 - 我的提示看起来像这样:
P2759474@RVPTINTCL415MQC 2017-10-06,11:20:18 ~/repos/jdk (master)
Run Code Online (Sandbox Code Playgroud)
它们甚至是彩色编码的,用户@ machine为绿色,时间戳为紫色,当前位置为黄色,任何git分支为蓝色.我只是有点恼火,我必须使用反引号而不是$()构造.
谁知道为什么?愿意帮助我理解吗?使用subshell命令解析复杂的提示值时,这只是一个问题,因为我想了解为什么它在那里很重要...一般的改进建议总是受到欢迎,而我们在它.
更新 -
目前,当我尝试使用$()时,我得到了很多
bash: command substitution: line 1: syntax error near unexpected token ')'
bash: command substitution: line 1: 'date +%Y-%m-%d,%H:%M:%S)'
bash: command substitution: line 1: syntax error near unexpected token ')'
bash: command substitution: line 1: '__git_ps1)'
Run Code Online (Sandbox Code Playgroud)
我的环境有
BASH_VERSINFO=([0]="4" [1]="3" [2]="42" [3]="5" [4]="release" [5]="x86_64-pc-msys")
BASH_VERSION='4.3.42(5)-release'
[ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no;
Run Code Online (Sandbox Code Playgroud)
这告诉我一些事情,也许......谢谢!
我有一个执行功能的子shell:
local thing=$( doFunc )
doFunc将日志输出发送到stderr(2),并且' thing'被分配给(1)doFunc上的输出stdout。
如何运行此行,但从stderr子shell 打印到stdout当前shell?
我在同一个目录中有 3 个脚本,请在下面找到 x.sh、y.sh 和 z.sh 的内容:-
x.sh :-
xData="DataOfX"
function xInit(){
echo "xInit : data of a >$xData<"
}
Run Code Online (Sandbox Code Playgroud)
y.sh :-
. x.sh
xInit
sh z.sh zInit
Run Code Online (Sandbox Code Playgroud)
z.sh :-
function zInit(){
echo "zInit of z"
xInit
}
$@
Run Code Online (Sandbox Code Playgroud)
执行
. y.sh
在同一目录中给出以下输出:-
xInit : data of a >DataOfX<
zInit of z
z.sh: line 3: xInit: command not found
Run Code Online (Sandbox Code Playgroud)
子 shell 进程如何访问在父 shell 中初始化的变量和函数?
在我的 Bash 会话中,我运行以下命令:
(echo $$ $BASHPID $-)
Run Code Online (Sandbox Code Playgroud)
我得到
22108 25602 himBH
Run Code Online (Sandbox Code Playgroud)
因此,我的子 shell 作为交互式 shell 运行。
如果我尝试在后台运行相同的命令
(echo $$ $BASHPID $-) &
Run Code Online (Sandbox Code Playgroud)
我得到相同的输出。为什么子shell作为交互式shell运行?
我使用的 Bash 命令:
$ ssh user@myserver.com ps -aux|grep -v \"grep\"|grep "/srv/adih/server/app.js"|awk '{print $2}'
6373
Run Code Online (Sandbox Code Playgroud)$ ssh user@myserver.com echo $(ps -aux|grep -v \"grep\"|grep "/srv/adih/server/app.js"|awk '{print $2}')
8630
Run Code Online (Sandbox Code Playgroud)第一个结果是正确的,第二个结果将改变我执行它的回显时间。但我不知道他们为什么不同。
我在做什么?
我的工作站的资源非常有限,因此我使用远程计算机来运行我的 Node.js 应用程序。ssh user@remotebox.com "cd /application && grunt serve"我在调试模式下运行它。当我命令Ctrl+时C,grunt 任务停止,但应用程序仍在调试模式下运行。我只想杀死它,我需要先获取PID。
我曾尝试寻找与此类似的问题来为我提供解决方案,但我找不到一个能完全回答我的确切问题的问题。
我想在 eval 调用中的子 shell 中运行命令,并获取子 shell 中调用的函数返回的状态代码。
例如,我调用这样的命令:
eval "$(some_command)"
if [ "${?}" -ne 0 ]
then
# do stuff if `some_command` returned status code not equal to zero
fi
Run Code Online (Sandbox Code Playgroud)
该some_command函数返回环境变量及其分配的列表,如下所示:
$ some_command # Execute some_command in a standard fashion without eval or subshell
some_env_variable='some_value'
another_env_variable='another value'
Run Code Online (Sandbox Code Playgroud)
目标是运行这个命令将这些环境变量添加到当前环境。唯一的方法是some_command在子 shell 中调用,并让 eval 评估结果输出。如果我在没有子 shell 的情况下执行此操作,eval 将简单地运行some_command,但不会评估其输出以将环境变量添加到当前环境。
这有效:
$ eval "some_command"
-bash: some_command: command not found
$ echo $?
127
Run Code Online (Sandbox Code Playgroud)
这有效:
$ $(some_command)
-bash: some_command: …Run Code Online (Sandbox Code Playgroud) 通常我通过子shell捕获输出:
result="$(command 2>&1)"
如果命令是source,则子外壳会吞下对脚本外壳环境的一些(全部?)更改。
如何将 的输出捕获source到变量中?
我知道在 Linux 中我可以使用该alias命令来获取已定义别名的列表。我现在尝试通过 Go 代码执行相同的操作:
func ListAlias() error {
out, err := exec.Command("alias").Output()
if err != nil {
fmt.Println(err)
return err
}
fmt.Println(out)
return nil
}
Run Code Online (Sandbox Code Playgroud)
但返回的所有内容是:
exec: "alias": executable file not found in $PATH
Run Code Online (Sandbox Code Playgroud)
我尝试寻找实际的二进制文件在哪里alias,但这也无济于事:
$whereis alias
alias:
Run Code Online (Sandbox Code Playgroud)
我考虑的替代方案是解析~/.bashrc文件以获取定义的别名列表,但我遇到过这种情况,其中bashrc列出了另一个custom_aliases.sh文件并且所有别名都列在那里。这就是为什么我尝试使用该alias命令列出所有别名。