LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2013, 02:06 PM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Setting different value types with multiple variables


Hello everyone,

I'm trying to set four variables while in one ssh connection.

The problem I'm running into is with my first variable. I want to know whether or not a file called 'shins' is present in the root directory.

When the file IS present, the variables work fine. But when the file is NOT present, there is nothing to put into it, so the script puts the value from the command below it in $VarZro.

I guess the best option would be to assign a "not present" status to $VarZro, like a "file not found" exit status. Can someone let me know how I would go about doing this?

Thanks.

Code:
#!/bin/bash


for STORE in `cat hostlist.txt`
do

                ping -c 1 $STORE > /dev/null 2>/dev/null
PingResult=$?
                if [ "$PingResult" -eq 0 ]; then
allVars=( $( ssh root@$STORE "\
                ls shins > /dev/null 2>/dev/null; \
                pwd ; \
                ls /etc/hosts ; \
                ls /etc/sysconfig/network ; " ) )
                        varZro="${allVars[0]}"
                        VarOne="${allVars[1]}"
                        VarTwo="${allVars[2]}"
                        VarThr="${allVars[3]}"
echo "=================="
echo "Hostname -  $STORE"
echo "shins    - $varZro"
echo "pwd      - $VarOne"
echo "etc/host - $VarTwo"
echo "network  - $VarThr"

 elif [ "$PingResult" -eq 1 ]; then
                echo "============================"
                echo "$STORE"
                echo "-------- Store Down --------"
        elif [ "$PingResult" -eq 2 ]; then
                echo "============================"
                echo "$STORE"
                echo "--- $STORE: UNKNOWN HOST ---"
fi
done
Output for command:
Code:
[techs@ts-01 bash]$ ./var-test.sh
==================
Hostname -  ft9587
shins    - /root
pwd      - /etc/hosts
etc/host - /etc/sysconfig/network
network  -
As you can see, the 'shins' echo shows the value for the pwd command, which assigns incorrect variables for all the variables that follow, and leaves the last variable blank.
 
Old 10-11-2013, 02:37 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How does it work even if the shins file is there? You're piping the output of the first ls directly to /dev/null. You're not going to get any output from the ls whether or not the file is there.

Anyway, how about just putting the "shins" check last? If there are only 3 variables than the shins file wasn't there and the variable will be empty, if there are 4 then it was there and the variable will be full.
 
Old 10-11-2013, 03:10 PM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
How does it work even if the shins file is there? You're piping the output of the first ls directly to /dev/null. You're not going to get any output from the ls whether or not the file is there.

Anyway, how about just putting the "shins" check last? If there are only 3 variables than the shins file wasn't there and the variable will be empty, if there are 4 then it was there and the variable will be full.
Thank you, that's a great idea to put the shins command last. (As for the /dev/null... not sure how I put that in there; my brain is fried today)

One more question about this concerning an empty last variable for a non-existent 'shins'. Can I still use the information that shins is non-existent? like for instance:
Code:
if [ "$VarThr" = 'Nothing' ]; then
I know the word "Nothing" won't work. How can I use the non-existence as a factor?

Thanks again.
 
Old 10-11-2013, 03:20 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Use the "-z" check

Code:
$ a="stuff"
$ if [[ -z $a ]]; then echo a is empty; else echo a is full; fi
a is full
$ a=""
$ if [[ -z $a ]]; then echo a is empty; else echo a is full; fi
a is empty
 
1 members found this post helpful.
Old 10-14-2013, 07:07 AM   #5
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Use the "-z" check

Code:
$ a="stuff"
$ if [[ -z $a ]]; then echo a is empty; else echo a is full; fi
a is full
$ a=""
$ if [[ -z $a ]]; then echo a is empty; else echo a is full; fi
a is empty
Perfect. This works great. Thanks for the assistance!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple types of passwords Pastwikowski Linux - Newbie 6 05-05-2013 03:50 PM
setting multiple java home variables in linux/.bashrc kaloon88 Linux - Newbie 3 11-30-2011 06:43 AM
LXer: C development on Linux - Types, variables, operators - III. LXer Syndicated Linux News 0 11-14-2011 06:31 PM
Networking across multiple OS's and Connection types. Jessendabomb Linux - Networking 5 10-12-2008 03:23 PM
returning different types of variables from a function knobby67 Programming 3 02-08-2008 08:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration