简体   繁体   English

循环执行命令以在BASH中打印和执行

[英]Looping over commands to print and execute in BASH

In BASH, I want to do something like this: 在BASH中,我想做这样的事情:

#!/bin/bash                                                                     

HOST="blah"
USER="foo"
REMOTE_ROOT="${HOST}:~${USER}/"

REP_NAME=`basename $1`
TARGET_NAME="${REP_NAME}.git"

CMD1="git clone --bare $1 $TARGET_NAME"
CMD2="touch ${TARGET_NAME}/git-daemon-export-ok"
CMD3="scp -r $TARGET_NAME $REMOTE_ROOT"
CMD4="rm -rf $TARGET_NAME"

for i in {1..4}
do
  CMD="${CMD${i}}"
  echo "$CMD"
  `$CMD`
done

That is to say, I want to loop over a list of commands, display the command being executed, then execute it. 也就是说,我要遍历命令列表,显示正在执行的命令,然后执行它。

I don't know how to do the double dereferencing (CMD="${CMD${i}}" isn't legal in BASH). 我不知道如何进行双重取消引用(CMD =“ $ {CMD $ {i}}”“在BASH中不合法)。

Alternately, I'd be happy to do something like: 或者,我很乐意做类似的事情:

for CMD in "CMD1 CMD2 CMD3 CMD4"
do
  echo $$CMD
done

but of course that isn't the right syntax, either. 但这当然也不是正确的语法。

CMDS[1]="git clone --bare $1 $TARGET_NAME"
CMDS[2]="touch ${TARGET_NAME}/git-daemon-export-ok"
CMDS[3]="scp -r $TARGET_NAME $REMOTE_ROOT"
CMDS[4]="rm -rf $TARGET_NAME"

# ...

for x in 1 2 3 4
do
    ${CMDS[x]};
done

您可以将CMD放在一个数组中吗?

You want the ${!parameter} syntax, works in bash atleast. 您需要$ {!parameter}语法,至少可以在bash中使用。 eg 例如

#!/bin/sh

CMD1="ls"
CMD2="pwd"

for CMD in {CMD1,CMD2} ; do
    echo ${!CMD}
    ${!CMD}
done

To do what you want use: 做您想使用的:

for i in 1 2 3 4; do
    eval cmd='$'CMD$i
    echo $cmd
    eval $cmd
done

For your alternate solution, you can use eval: 对于替代解决方案,可以使用eval:

for CMD in CMD1 CMD2; do
    eval '$'$CMD
done

y'all forgot seq(1). 你们都忘记了seq(1)。 it takes three arguments: seq START INTERVAL END and produces a new line delimited list of between (inclusively) START and END, so you could do: 它包含三个参数:seq START INTERVAL END,并产生一个新行分隔列表(介于(包括)START和END之间),因此您可以执行以下操作:

for i in `seq 1 1 4`
do
   echo...

also: 也:

if a relative path is given after host: , it's assumed to be the login user's home directory. 如果在host:之后给出了相对路径,则假定它是登录用户的主目录。 so you can just do: 所以你可以这样做:

REMOTE_ROOT="${HOST}:"

This is more of a debugging/troubleshooting feature, and doesn't exactly answer your question, but I find it useful: 这更多是一种调试/故障排除功能,不能完全回答您的问题,但是我发现它很有用:

#!/bin/bash

# Enable command echoing
set -x 

pwd
uname -a

# Disable command echoing
set +x

echo 'Command echoing off now'

Example run: 示例运行:

~$ ./foo.sh 
+ pwd
/home/jason
+ uname -a
Linux jpc 2.6.26-2-amd64 #1 SMP Sun Jun 21 04:47:08 UTC 2009 x86_64 GNU/Linux
+ set +x
Command echoing off now
~$ 

Or if you had a similar file: 或者,如果您有类似的文件:

#!/bin/bash

pwd
uname -a

You could use bash -x to do the same thing without having to modify the file: 您可以使用bash -x来执行相同的操作,而无需修改文件:

~$ bash -x ./bar.sh 
+ pwd
/home/jason
+ uname -a
Linux jpc 2.6.26-2-amd64 #1 SMP Sun Jun 21 04:47:08 UTC 2009 x86_64 GNU/Linux
~$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM