[SOLVED] Code does not run when assigned to a variable

I am more of a newbie, but wanted to post this in this forum as I was afraid no one would look at it in unix forums as it concerns shell scripting. I have a shell script that now runs fine with the exclusion of one line:

x=`su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root/"`;echo "$x"

This command will run and ssh to the server. It prints out /vol/root after it sshs to servery. However, it does not print out a list of the contents of /vol/root. However, if I run the command WITHOUT assigning it to a variable, like this:

su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root/"

It runs and prints all the directories under root. What is it about this command that you cannot assign it to a variable and it won't print out the contents? The bplist -l -R options are supposed to do that and they do so long as I do not assign the command to a variable and echo it.

Any help would be welcome from you gurus!

Not sure what OS or shell you're using but the output might be a bit much for a normal variable. Instead, you could try storing the returned output in an array:

declare -a array=( $(su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root/") )

Then iterate the array to display the results:

for (( i = 0; i < ${#array[@]}; i++ ))
do
    echo ${array[$i]}
done

This is the bash root shell. I have tried to just echo an array, and it completes successfully but then still gives the same input as before. I am also not able to make the for loop work, see below. This is probably because I do not have much experience with arrays. But the basic echo array returns the /vol/root without any subdirectories. Maybe I am not making the loop for the iteration correctly.

Basic echo array:

declare -a array=( $(su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/20
12 -e 11/01/2012 -e 11/01/2012 /vol/root/") )
echo ${array[$i]}

this gives errors:

declare -a array=( $(su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/0
12 -e 11/01/2012 -e 11/01/2012 /vol/root/") )
for (( i = 0; i < ${#array[@]}; i++ ))
do
 echo ${array[$i]}
done

Errors:
+ declare -a 'array=( drwxr-xr-x root root 4096 May 25 2012 /vol/root/ )'
testing_1130: line 2: syntax error near unexpected token `(('
testing_1130: line 2: `for (( i = 0; i < ${#array[@]}; i++ ))'

Again, any help you gurus have is always appreciated.

That is a very big and complicated command. I would try and unwrap that a little, not jam everything on one line.

Also, you can't jam multiple lines of text in a variable and expect them to work right. Quoting them is a bit of a hassle. Save it in a file instead -- that makes it easy to use in a loop or transfer into anything else.

Try this command:

su nbadmin -c "ssh -t servery exec /bin/sh -s" > /tmp/$$ <<EOF
/usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root
EOF

echo "Contents of /vol/root"
cat /tmp/$$

rm -f /tmp/$$

Corona:

Thanks for all the help! This worked! I did not realize about the line length and a lot of my scripts work but the lines are too long as you said. I only had one more question, I am trying to assign it to the nbadmin command to a variable as this script actually asks for user input. So actually it was

echo -e "do you want to run this command?"
read runthiscommand
runthiscommand=` su nbadmin -c "ssh -t severy /usr/openv/netbackup/bin/bplist -C servery 5 -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root/"`;echo "$runthiscommand"

I will check what you wrote and see if I can incorporate it to the user input part of the script... If any more suggestions, appreciate it!:o This forum is very friendly to newbies!

I have no idea what you are even trying to do there. You read the variable, then immediately overwrite it with something else without using it for anything. What is the intent?

I got this working for my user input script and am very happy! Thanks to all who helped!:b:

You can also try this way

x=$(su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C servery -t 19 -l -R -s 11/01/2012 -e 11/01/2012 /vol/root/")
 
echo $x