Register
It is currently Fri Oct 24, 2014 5:18 am

How do I read an external file to use it as a variable?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Wed Sep 04, 2013 2:28 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Hello,

I've created the following script to reconnect my raspberry pi (running Debian) to a VM using rdesktop:

#!/bin/sh
# Check if rdesktop is running
LOGFILE=~/scripts/check.log
if pgrep rdesktop > /dev/null
then
echo "$(date "+%m%d%Y %T") : rdesktop is already running." >> $LOGFILE 2>&1
exit 0
else
echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to restart now." >> $LOGFILE 2>&1
rdesktop -u User 192.168.100.100
exit 0
fi

What I would like to have is the IP address saved in a file (maybe the same folder where the script lives) and when the script is run, the script will read in the IP address from that file.

How would I do that please?

Thank you.


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 3:26 pm   

Joined: Wed Sep 04, 2013 2:32 pm
Posts: 4
I'm not sure what you mean but hope this helps..
Code:
function myip {
  myip=`elinks -dump http://checkip.dyndns.org:8245/`
  echo -e "text\n" >> ip.txt
  echo "${myip}"  >> ip.txt
}

cat ip.txt


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 3:33 pm   

Joined: Wed Sep 04, 2013 2:32 pm
Posts: 4
kkv19 wrote:
I'm not sure what you mean but hope this helps..
Code:
function myip {
  myip=`elinks -dump http://checkip.dyndns.org:8245/`
  echo -e "${myip}"  >> ip.txt
}
myip
cat ip.txt
}

cat ip.txt


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 3:34 pm   

Joined: Wed Sep 04, 2013 2:32 pm
Posts: 4
I'm not sure what you mean but hope this helps..
Code:
function myip {
  myip=`elinks -dump http://checkip.dyndns.org:8245/`
  echo -e "${myip}"  >> ip.txt
}
myip
cat ip.txt


Top
 Profile  
 PostPosted: Wed Sep 04, 2013 10:50 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Hello

Not quite what I was looking for. Instead of finding my current IP, I'm looking to read the IP from a file as a variable. So my code would look something like this:

Code:
#!/bin/sh
# Check if rdesktop is running
LOGFILE=~/scripts/check.log
if pgrep rdesktop > /dev/null
then
echo "$(date "+%m%d%Y %T") : rdesktop is already running." >> $LOGFILE 2>&1
exit 0
else
echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to restart now." >> $LOGFILE 2>&1
rdesktop -u User $Variable
exit 0
fi


There would be a file that contains just the IP address:
contents of IPfile:
Code:
192.168.100.100


I would like assistance with how to modify my script in such a way to read the contents of IPfile and use the IP address in this file as my $Variable in my script.

Thank you.


Top
 Profile  
 PostPosted: Thu Sep 05, 2013 3:09 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
One method, might work for you;

Code:
var=$(<file)


DW


Top
 Profile  
 PostPosted: Thu Sep 05, 2013 5:30 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

as the shebang is sh, previous command might not work,
in this case:
Code:
read var < file


Top
 Profile  
 PostPosted: Fri Sep 06, 2013 8:02 am   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Yep, that did the trick...Thanks Watael for the assistance.

Looking to enhance my script further now. Now that I know I can read in a variable from a file to my script, I'd like to have in that file a list of hostnames and beside each hostname the IP address. Then in my script I'll need an if statement or case statement to check for the hostname of the server and use the IP address from the file. Looking at the Bash reference manual I see there is a global variable for hostname called HOSTNAME.

Any help you can provide to get me started would be appreciated.

Thank you.


Top
 Profile  
 PostPosted: Sun Sep 08, 2013 1:16 pm   

Joined: Sun Aug 18, 2013 4:27 pm
Posts: 10
Hello,

I'm still working on my script. So far I've got it working where it reads the hostname and finds the IP to use based on the case statement. This is what I have so far.

Code:
#!/bin/sh
# Check if rdesktop is running
LOGFILE=/home/rpitc/scripts/check.log
read var < ipfile
THISHOST=$(hostname)
if pgrep rdesktop > /dev/null
then
        echo "$(date "+%m%d%Y %T") : rdesktop is already running." >> $LOGFILE$
        exit 0
else
        echo "$(date "+%m%d%Y %T") : rdesktop has Stopped. Attempting to resta$
        case $THISHOST in
          (rpiA) rdesktop -z -u user -p password 10.100.100.10;;
          (rpiB) rdesktop -z -u user -p password 10.100.100.11;;
          (rpiC) rdesktop -z -u user -p password 10.100.100.12;;
        esac
        exit 0
fi


Right now my code is not using the line: read var < ipfile

But I would like list all my server IP's in the ipfile like this:

Code:
rpiA="10.100.100.10"
rpiB="10.100.100.11"
rpiC="10.100.100.12"


And then modify my code to reference the variables in ipfile in my script.

Thoughts?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP