[SOLVED] Code does not run when assigned to a variable | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 22 April 2014, 9:56 PM EDT
Posts: 136
Thanks: 54
Thanked 12 Times in 2 Posts
[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!
Sponsored Links
    #2  
Old 11-30-2012
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 24 April 2014, 6:48 PM EDT
Location: East Coast
Posts: 566
Thanks: 0
Thanked 161 Times in 147 Posts
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:


Code:
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

Sponsored Links
    #3  
Old 11-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 22 April 2014, 9:56 PM EDT
Posts: 136
Thanks: 54
Thanked 12 Times in 2 Posts
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:


Code:
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:

Code:
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.

Last edited by joeyg; 11-30-2012 at 02:20 PM.. Reason: Please wrap data and sripts with CodeTags
    #4  
Old 11-30-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 April 2014, 7:36 PM EDT
Location: Saskatchewan
Posts: 18,539
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
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:


Code:
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/$$

Sponsored Links
    #5  
Old 11-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 22 April 2014, 9:56 PM EDT
Posts: 136
Thanks: 54
Thanked 12 Times in 2 Posts
This worked!

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! This forum is very friendly to newbies!
Sponsored Links
    #6  
Old 11-30-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 April 2014, 7:36 PM EDT
Location: Saskatchewan
Posts: 18,539
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
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?
Sponsored Links
    #7  
Old 11-30-2012
newbie2010 newbie2010 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 22 April 2014, 9:56 PM EDT
Posts: 136
Thanks: 54
Thanked 12 Times in 2 Posts
I got this working for my user input script and am very happy! Thanks to all who helped!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Why code run not correctly FUTURE_EINSTEIN UNIX for Dummies Questions & Answers 4 07-12-2012 08:56 AM
How do you check if a variable has been assigned? Fred63528 UNIX for Dummies Questions & Answers 3 12-04-2011 11:32 AM
Check if a variable has a value assigned? sumeet Shell Programming and Scripting 3 05-03-2009 11:48 PM
Return code of command assigned to variable browndr Shell Programming and Scripting 2 08-05-2008 08:26 AM
What does $? mean when assigned to a variable? ranjita.c UNIX for Dummies Questions & Answers 3 08-24-2006 09:45 AM



All times are GMT -4. The time now is 07:38 PM.