Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ubuntu中,如何在bash for循环中生成别名?_Bash_Variables_Ubuntu_For Loop_Alias - Fatal编程技术网

在Ubuntu中,如何在bash for循环中生成别名?

在Ubuntu中,如何在bash for循环中生成别名?,bash,variables,ubuntu,for-loop,alias,Bash,Variables,Ubuntu,For Loop,Alias,我有一个脚本来帮助我测试并发性。我用屏幕一次运行一堆东西。但在所有这些情况下,我不知道如何使用for$I变量生成别名(或函数?) #!/bin/bash # alias p='. ./p.sh' # Run a bunch of apps in separate screens. export NUM_MUS=3 for (( i = 1; i <= $NUM_MUS; ++i )) do echo $i cd ~/mutest/mu$i screen -mS mu$

我有一个脚本来帮助我测试并发性。我用屏幕一次运行一堆东西。但在所有这些情况下,我不知道如何使用for$I变量生成别名(或函数?)

#!/bin/bash
# alias p='. ./p.sh'

# Run a bunch of apps in separate screens.

export NUM_MUS=3
for (( i = 1; i <= $NUM_MUS; ++i ))
do
   echo $i
   cd ~/mutest/mu$i
   screen -mS mu$i -h 10000 ./app

# How do I make an alias to let me easily access each screen?
# None of these work.
#
#   alias mu$i='screen -dr mu$i'
#   eval
#   function mu$i () { screen -dr mu$i }

   # ^AD to get out
done

# Have a way of killing them all.

function mustop ()
   {
   for (( i = 1; i <= $NUM_MUS; ++i ))
   do
      screen -r mu$i -X quit
   done
   screen -ls
   }

但它仍然不能回答我使用for循环变量生成buncha别名/函数的问题。我并不是说这是最聪明的方法,但如果我真的想这样做,我会怎么做?

变量在双引号内展开,而不是在单引号内展开

alias mu$i="screen -dr mu$i"

另外,请确保使用
源脚本名运行脚本,这样别名将在原始shell中定义,而不仅仅是在执行脚本的shell中定义。

很好,解决了这个问题!为什么在解释如何使用别名的任何页面中都没有提到这一点?这并不是别名特有的,它适用于shell中所有的字符串。我第一次意识到这一点。所以它就像PHP一样,相反。我认为PHP是从Perl复制的,Perl是从shell复制的。
alias mu$i="screen -dr mu$i"