代码之家  ›  专栏  ›  技术社区  ›  Logan Lee

在Bash脚本中执行从参数接收到的命令

  •  0
  • Logan Lee  · 技术社区  · 5 年前

    我有一个Perl脚本,它为我提供了一个定义的随机数列表,这些随机数对应于文件的行。接下来,我想使用 sed .

    #!/bin/bash
    count=$(cat last_queries.txt | wc -l)
    var=$(perl test.pl test2 $count)
    

    变量 var 返回如下输出: cat last_queries.txt | sed -n '12p;500p;700p' .问题是我无法运行最后一个命令。我试过了 $var ,但输出不正确(如果我手动运行该命令,它工作正常,因此没有问题)。正确的方法是什么?

    附言:当然,我可以用Perl完成所有的工作,但我正在尝试用这种方式学习,因为它可以在其他情况下帮助我。

    0 回复  |  直到 4 年前
        1
  •  238
  •   Peter Mortensen Pieter Jan Bonestroo    6 年前

    你只需要做:

    #!/bin/bash
    count=$(cat last_queries.txt | wc -l)
    $(perl test.pl test2 $count)
    

    但是,如果您想稍后调用Perl命令,这就是为什么要将其分配给变量,那么:

    #!/bin/bash
    count=$(cat last_queries.txt | wc -l)
    var="perl test.pl test2 $count" # You need double quotes to get your $count value substituted.
    
    ...stuff...
    
    eval $var
    

    根据Bash的帮助:

    ~$ help eval
    eval: eval [arg ...]
        Execute arguments as a shell command.
    
        Combine ARGs into a single string, use the result as input to the shell,
        and execute the resulting commands.
    
        Exit Status:
        Returns exit status of command or success if command is null.
    
        2
  •  188
  •   Peter Mortensen Pieter Jan Bonestroo    4 年前

    你可能在找 eval $var .

        3
  •  2
  •   Benjamin W.    10 年前
    line=$((${RANDOM} % $(wc -l < /etc/passwd)))
    sed -n "${line}p" /etc/passwd
    

    就用你的文件吧。

    在本例中,我使用了文件/etc/password,使用了特殊变量 ${RANDOM} (我在这里学到的)以及 sed 表达式,唯一的区别是我使用双引号而不是单引号来允许变量展开。

        4
  •  2
  •   Ajedi32    10 年前

    如果有多个变量包含正在运行的命令的参数,而不仅仅是一个字符串,那么应该 不 直接使用eval,因为它在以下情况下会失败:

    function echo_arguments() {
      echo "Argument 1: $1"
      echo "Argument 2: $2"
      echo "Argument 3: $3"
      echo "Argument 4: $4"
    }
    
    # Note we are passing 3 arguments to `echo_arguments`, not 4
    eval echo_arguments arg1 arg2 "Some arg"
    

    结果:

    Argument 1: arg1
    Argument 2: arg2
    Argument 3: Some
    Argument 4: arg
    

    请注意,尽管“Some arg”作为单个参数传递, eval 读两遍。

    相反,您可以将字符串用作命令本身:

    # The regular bash eval works by jamming all its arguments into a string then
    # evaluating the string. This function treats its arguments as individual
    # arguments to be passed to the command being run.
    function eval_command() {
      "$@";
    }
    

    请注意 评估 新的 eval_command 功能:

    eval_command echo_arguments arg1 arg2 "Some arg"
    

    结果:

    Argument 1: arg1
    Argument 2: arg2
    Argument 3: Some arg
    Argument 4:
    
        5
  •  0
  •   7beggars_nnnnm    4 年前

    更好的方法

    使用函数:

    # define it
    myls() {
        ls -l "/tmp/test/my dir"
    }
    
    # run it
    myls
    

    使用数组:

    # define the array
    mycmd=(ls -l "/tmp/test/my dir")
    
    # run the command
    "${mycmd[@]}"
    
        6
  •  0
  •   user8766193 user8766193    4 年前

    在shell脚本中执行string命令有两种基本方法,不管它是否作为参数给出。

    COMMAND="ls -lah"
    $(echo $COMMAND)
    

    或

    COMMAND="ls -lah"
    bash -c $COMMAND
    
    推荐文章