简体   繁体   English

在Ubuntu中,如何在bash for循环内创建别名?

[英]In Ubuntu, how do I make an alias inside a bash for loop?

I have a script to help me with testing concurrency. 我有一个脚本可以帮助我测试并发性。 I use screen to run a bunch of things at once. 我使用屏幕一次运行一堆东西。 But within all that, I can't figure out how make an alias (or function?) using the for $i variable. 但是,在所有这些之中,我不知道如何使用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
   }

Please note that I am NOT asking about taking parameters. 请注意,我并不是在询问参数。 I don't want parameters ... unless there IS a way to make it work with parameters? 我不想要参数...除非有一种方法可以使其与参数一起使用?

... ...

Okay, I can add this line, and it'll work "good enough" 好的,我可以添加这一行,它将“足够好”地工作

function mu { screen -dr mu$1 }

But it still doesn't answer my question of using for loop variables to make a buncha aliases/functions. 但是它仍然不能回答我使用for循环变量来创建Bunka别名/函数的问题。 I'm not saying that's this is smartest way to do things, but if I did want to do this, how would I do it? 我并不是说这是最聪明的做事方式,但是如果我确实想这样做,我将如何做?

Variables are expanded inside double quotes, not inside single quotes. 变量在双引号内扩展,而不在单引号内扩展。

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

Also, make sure you run the script using source scriptname , so the aliases will be defined in your original shell, not just the shell executing the script. 另外,请确保使用source scriptname运行脚本,以便别名将在原始外壳程序中定义,而不仅仅是在执行脚本的外壳程序中定义。

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

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