executing shell command in background from script

前端 未结 4 494
误落风尘
误落风尘 2020-11-27 17:40

how can I execute a shell command in the background from within a bash script, if the command is in a string?

For example:

#!/bin/bash
cmd=\"nohup my         


        
4条回答
  •  情歌与酒
    2020-11-27 18:13

    This works because the it's a static variable. You could do something much cooler like this:

    filename="filename"
    extension="txt"
    for i in {1..20}; do
        eval "filename${i}=${filename}${i}.${extension}"
        touch filename${i}
        echo "this rox" > filename${i}
    done
    

    This code will create 20 files and dynamically set 20 variables. Of course you could use an array, but I'm just showing you the feature :). Note that you can use the variables $filename1, $filename2, $filename3... because they were created with evaluate command. In this case I'm just creating files, but you could use to create dynamically arguments to the commands, and then execute in background.

提交回复
热议问题