Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to creat a script to run an another script with a several parameter

exp=([1]=bloc [2]=ins [3]=rep [4]=op)
for j in ${!exp[*]}
do
    arr=([1]=mem [2]=gen [3]=usr)
    for i in ${!arr[*]}
    do
    var="bash createGnuploat.sh "${exp[j]}" ../Result/ 0" ${arr[i]} ${exp[j]}
    $var
    done
done

And i have this error :

run.sh: line 9: =bash createGnuploat.sh op ../Result/ 0: Aucun fichier ou dossier de ce type

What's problem please ?

share|improve this question
1  
Read BashFAQ 50 if you haven't already. Shell variables are not suitable containers for whole command-lines, in general. What are you trying to accomplish? –  jw013 Sep 17 '12 at 15:10
2  
In assignments, do not use $ for variables you assign to: var="...". –  choroba Sep 17 '12 at 15:12
    
Also, don't give bash scripts .sh extensions - it's misleading. –  jw013 Sep 17 '12 at 15:13
    
@jw013 I always do that as well, what you suggest? –  warl0ck Sep 17 '12 at 15:23
    
@warl0ck I normally just leave off the extension since it's the shebang line that matters anyways. However if I want an explicit reminder that "this is a bash script", in the name of the file, .bash works. Either way is better than .sh which implies /bin/sh. –  jw013 Sep 17 '12 at 15:25
add comment

2 Answers 2

up vote 1 down vote accepted

If you need to store a command in a variable, don't use a string, that just can't work. See Bash FAQ #50. Use a function or an array.

Your line is parsed as the assignment var="bash createGnuploat.sh "${exp[j]}" ../Result/ 0" followed by the two words ${arr[i]} (which will be taken as a command name) and ${exp[j]} (which will be the first and only argument to the command). Check the syntax highlighting in your question or in your editor, it shows what is inside the quoted string.

Always use double quotes around variable substitutions, e.g. "$foo". Otherwise the value of the variable is split into words that are interpreted as glob patterns. (Omit the double quotes in the 0.01% of the cases where this is desired behavior.) For an array, use "${foo[@]}" to have each element of the array in a separate word ("${foo[*]}" is a single word with the elements of the array separated by spaces; if you leave out the quotes, then each element is broken into separate words that are interpreted as glob patterns).

Here's your snippet rewritten using a function:

create_plot () {
  bash createGnuploat.sh "${exp[$2]}" ../Result/ 0 "${arr[$1]}" "${exp[$2]}"
}

for j in "${!exp[@]}"
do
    arr=([1]=mem [2]=gen [3]=usr)
    for i in "${!arr[@]}"
    do
      create_plot "$i" "$j"
    done
done

For the rare cases where it makes sense to use an array variable instead of a function:

for j in "${!exp[@]}"
do
    arr=([1]=mem [2]=gen [3]=usr)
    for i in "${!arr[@]}"
    do
      var=(bash createGnuploat.sh "${exp[j]}" ../Result/ 0 "${arr[i]}" "${exp[j]}")
      "${var[@]}"
    done
done
share|improve this answer
add comment

There are multiple syntax and quoting issues in the line

$var="bash createGnuploat.sh "${exp[j]}" ../Result/ 0" ${arr[i]} ${exp[j]}

Here is a cleaner version of the script in your question:

for j in bloc ins rep op; do
        for i in mem gen usr; do
                bash createGnuploat.sh "$j" ../Result/ 0 "$i" "$j"
        done
done

I don't know what the arguments are supposed to be for createGnuploat.sh so you'll have to check that yourself.

share|improve this answer
    
The problem is not in createGnuploat.sh an not arguments, i can run it directly with same arguments. But in my case i want to know how to run a commande (bash createGnuploat.sh "$j" ../Result/ 0 "$i" "$j" ) writed in bash file ? –  user15992 Sep 17 '12 at 15:31
    
@user15992 Edit your question and insert a description of what you are trying to accomplish. –  jw013 Sep 17 '12 at 15:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.