ambikanair
ambikanair

Reputation: 4590

Writing a Nested shell script- Unable to pass argument

I am trying to create a shell script using another script. Following is the code.

#!/bin/bash
count=$#
cat << EOF > /tmp/kill_loop.sh 
#!/bin/bash

while true;
do
    for i in "$@"
    do
       echo $i
    done

done 
EOF

When I see kill_loop.sh , "$i" is empty.

#!/bin/bash
while true;
do
    for i in "one two three"
    do
       echo

    done
done

I want "$i" to be printed as such in kill_loop.sh file so that if i execute kill_loop.sh, it echoes the value 'one','two' and 'three'

Upvotes: 2

Views: 1431

Answers (2)

Marty McGowan
Marty McGowan

Reputation: 366

here is the "foreach" function:

function foreach
{ 
    typeset cmd=$1;
    shift;
    for arg in "$@";
    do
        $cmd $arg;
    done
}

Upvotes: 0

Moldova
Moldova

Reputation: 1651

Your "outer" shell script is interpreting $i as if it were one of its own variables, which isn't set, thus it evaluates to nothing. Try escaping the $ so the outer shell doesn't expand it:

echo \$i

Upvotes: 3

Related Questions