LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   not able to read variable value which is sed into a file (https://www.linuxquestions.org/questions/linux-newbie-8/not-able-to-read-variable-value-which-is-sed-into-a-file-4175546306/)

vjlxmi 06-24-2015 08:45 AM

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

pan64 06-24-2015 09:04 AM

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.

vjlxmi 06-24-2015 09:31 AM

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

grail 06-24-2015 09:49 AM

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.

pan64 06-25-2015 12:34 AM

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)

Keith Hedger 06-25-2015 01:16 AM

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.


All times are GMT -5. The time now is 09:27 AM.