1

I'm stuck with a rather silly bash script:

CMD="get database" /   #embedded,specific platform command
DATA="tree path Uo5 Uu7"

custom_command='grep -i Arte | awk -F '[:]' '{print $2}'

And this is what I want to do:

VAR=`$CMD "show data $DATA" | $custom_command`   <--not working
VAR=`$CMD "show data $DATA" | grep -i Arte | awk -F '[:]' '{print $2}'` <--working

Using $custom_command breaks the script. How can i use $custom_command to VAR?

I use custom_command to avoid using the same string over and over again. Can you help?

aprin
  • 123
  • 1
  • 9

3 Answers3

3

You do not want to assign commands to variables in bash. You want to write functions.

custom_command() {
    grep -i Arte | awk -F '[:]' '{print $2}'   
}
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Indeed. i tried that but i don't know how to use the function to the output..:/ – aprin Feb 02 '14 at 17:10
  • Just like any other command. `... | custom_command` will do. BTW `$CMD` also should be a function. – n. m. could be an AI Feb 02 '14 at 17:27
  • works perfectly, thanks..!! Now is there a special reason to use functions instead of "eval" below?? ;) Which is the most "elegant" way? – aprin Feb 02 '14 at 17:39
  • 1
    http://stackoverflow.com/questions/17529220/why-should-eval-be-avoided-in-bash-and-what-should-i-use-instead – n. m. could be an AI Feb 02 '14 at 17:46
  • 2
    `eval` has a well-deserved reputation as a bug magnet -- see [this answer](http://stackoverflow.com/questions/9308606/usr-bin-find-cannot-build-its-arguments-dynamically/9322047#9322047) for a example. If you're using the same command over and over, I'd use a function instead -- that's what they're for. Also, see [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) for more discussion and options. – Gordon Davisson Feb 02 '14 at 18:10
0

You can do this:

custom_command="awk -F':' '/Arte/ {print \$2}'"
var=$(eval "$CMD 'show data $DATA' | $custom_command")

OR else without eval:

custom_command="/Arte/ {print \$2}"
var=$("$CMD" 'show data $DATA' | awk -F':' "$custom_command")
  • You aren't actually supplying any input file/data to your grep command
  • However there is no real need of grep here since awk can handle search part also
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, but actually the command is bigger, just posted a shortcut. So i really need to have the custom command inside a variable..that's the big issue. (i will use your way, to cut it shorter - by the way :) ) You think that i could just use the custom command and then add a < having as input this one: < $CMD "show data $DATA" – aprin Feb 02 '14 at 15:56
  • I want $custom_command, not $CMD ;) Pipe does not work for this: grep: |: No such file or directory grep: awk: No such file or directory grep: '[=,]': No such file or directory grep: '{print: No such file or directory grep: $2}'|: No such file or directory grep: sort: No such file or directory grep: |: No such file or directory grep: grep: No such file or directory grep: [0-9]: No such file or directory – aprin Feb 02 '14 at 16:41
  • You can name your variable whatever you want and my answer doesn't change part. My answer is only for fixing your grep and doing that in awk itself. – anubhava Feb 02 '14 at 16:44
  • If you post your script in your question I can investigate and suggest. – anubhava Feb 02 '14 at 16:44
  • i edited my initial question to make it more clear! I want to replace the second part of VAR with a string variable! – aprin Feb 02 '14 at 16:49
0

You need to do eval. Something like:

FINAL_CMD="$CMD show data $DATA | $custom_command"
VAR=$(eval $FINAL_CMD)

Or to adapt it more to your question, try:

VAR=`$CMD "show data $DATA" | eval $custom_command`
Jakub Kotowski
  • 7,411
  • 29
  • 38