LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2015, 08:45 AM   #1
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Rep: Reputation: Disabled
not able to read variable value which is sed into a file


Hello all,
I have a file test.sh in which I am reading the disk size in two systems, system1 and system2 and storing them in variables s1 and s2
then I am "sed"ing the values into the same file using commands
Code:
sed "6i \server1=\"${s1}\"" -i /Server1/test.sh
for system1 and
Code:
sed "7i \server2=\"${s2}\"" -i /Server1/test.sh
for system2.
But when i try to read the value of the variables server1 and server2 I get empty strings i.e. the value is null..
why so?? I know it sounds complicated but can anyone help me?

Thanks in Advance
 
Old 06-24-2015, 09:04 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
would be nice to see your test.sh before and after that sed command (to check what's happened)
also you can try to add set -xv at the beginning of your script.
 
Old 06-24-2015, 09:31 AM   #3
vjlxmi
Member
 
Registered: Aug 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
code:
Quote:
#! /bin/bash -xv
s1=`fdisk -l | grep 'GB' | grep 'sd'`
sed "6i \server1="${s1}"" -i /Server1/test.sh
s2=`ssh user@server2 "fdisk -l | grep 'GB' | grep 'sd'"`
sed "7i \server2="${s2}"" -i /Server1/test.sh
echo server1=$server1
echo server2=$server2
if [ "$server1" == "$server2" ]
then
do something
fi

Last edited by vjlxmi; 06-24-2015 at 09:35 AM.
 
Old 06-24-2015, 09:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think you have missed the point. The sed's will alter the files called on and add the serverN=val line. Within your script, server1 and server2 variables are never set.
 
Old 06-25-2015, 12:34 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
grail means this script will alter the file named test.sh. Probably this script is that one, I have no idea. But in that case the running program cannot be altered, just the file (so you need to start test.sh after that modification)
 
Old 06-25-2015, 01:16 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
From what the op has said and posted this is definatly self modifying code which you cant do with bash and is a realy old fashioned and crappy way of programming. Self modifying code may have been tolerable in the past when RAM and storage space was at a premium but not on a modern system.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using sed with variable from a file lolulove Programming 3 05-30-2013 05:25 PM
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
[SOLVED] Using a variable in sed to pick a specific line from a text file, bash Sunvic Linux - Newbie 10 08-12-2012 11:03 AM
[SOLVED] sed append string in variable to last line of file. SilversleevesX Linux - Newbie 7 11-27-2011 11:13 PM
how-to make sed read 1 random line into a file and parse it ot a variable?? Speedy2k Linux - Newbie 7 05-24-2009 11:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:50 PM.

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