Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to edit file from script

Status
Not open for further replies.

cp2000

Technical User
Dec 31, 2002
81
US
I need to be able to edit a file from my script. I think I have all the pieces togather except.....How do I pass a variable to the ex editor. Right now I am trying --

ex $FILENAME <<EOF
1
/$OLD_DATE
.s/$NEW_DATE/$NEW_DATE/
wq
EOF


If I have the wrong forum I would appreciate a redirect...
 
what variable are you trying to pass?
It seems like you already have variable in theside the here-doc for the 'ex'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
$OLD_DATE and $NEW_DATE are defined earlier in the script. I need to pass them to the ex commandline. The above is how I am trying (unsuccessfully) to do it.

I see your confusion though. I got my example wrong. Try this:

ex $FILENAME <<EOF
1
/$OLD_DATE
.s/$OLD_DATE_DATE/$NEW_DATE/
wq
EOF
 
you're ALREADY passing the variable into the herein-doc portion of 'ex'. The 'herein-doc' supplied variables are evaluated by the surrounding shell _prior_ to the execution [as for any other shell command].

I think the problem is with your 'ex' content. HOW are you trying to edit your file - what's the goal here?

You could try similar sequence of command with 'vi' to iron out the details FIRST and then try it out on with the ex's herein-doc.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Code:
.s/$OLD_DATE_DATE/$NEW_DATE/
            ^^^^^
Typo ?

Hope This Help
PH.
 
OK, Let me try again...One of these days my typing will impprove!!!
Yes, PHV, that is a typo. Unfortunately it's in the sumbission not the code.

THE BIG PICTURE
I am running a Bash shellscript on a RH8 system to backup a remote Solaris 8 system. The actual backup works fine. (tar over SSH). The final touch though is for the script to update the data file (dumpdates) on the remote system. I realize that the dumpdates file is used by the ufs* commands. They all a part of my backup process.

I am using a commandline script to feed the ex edit the information to edit the file. It works fine when I plug the values directly in. But when I try to pass variables to it the script goes no-op....
 
Are the variables exported ?

Hope This Help
PH.
 
I didn't think they needed to be when they were using within the script. But I will try it...
 
#!/bin/bash
# This script allows for backup of remote *UX machines
# It does,however, assume the remote machine has
# a RSA trust with this machine
# usage: backup 'remote host (FQN)' 'Remote directory to backup' 'Local directory copy to'
# Example: backup /usr /data

# check that the user is root

if [ $(id -un) != &quot;root&quot; ]; then
echo &quot;You need to be root to backup everything.&quot;
exit
fi

# Define variables
LOCALDIRECTORY=$3
REMOTEDIRECTORY=$2
REMOTELOGIN=root
REMOTEHOST=$1
REMOTEHOSTNAME=`echo $REMOTEHOST |gawk -F. '{print $1}'`
REMOTEDIRECTORYNAME=`echo $REMOTEDIRECTORY |gawk -F/ '{print $NF}'`
#TARFILENAME=&quot;$REMOTEDIRECTORYNAME-$(date +%Y%m%d).tar&quot;
TARFILENAME=&quot;$REMOTEDIRECTORYNAME.tar&quot;


# Run backup
echo &quot;Preparing to backup $1 to $2&quot;

rm -f $LOCALDIRECTORY/$REMOTEHOSTNAME/$TARFILENAME{,.gz}
ssh $REMOTELOGIN@$REMOTEHOST tar -cf - $REMOTEDIRECTORY |cat >$LOCALDIRECTORY/$REMOTEHOSTNAME/$TARFILENAME
echo &quot;...Done&quot;
rm $TEMPFILE

# Compress results
echo -n &quot;Compressing $LOCALDIRECTORY/$REMOTEHOSTNAME/$TARFILENAME&quot;
gzip $LOCALDIRECTORY/$REMOTEHOSTNAME/$TARFILENAME
echo &quot;...Done&quot;
date

exit 0
 
??
I'm confused: where are the ex problem in this script ?

Hope This Help
PH.
 
Oooppps wrong script...
#!/bin/bash
# This script edits a file
# usage: myedit filename

# check that the user is root

if [ $(id -un) != &quot;root&quot; ]; then
echo &quot;You need to be root to backup everything.&quot;
exit
fi

# Define variables
FILENAME=&quot;dumpdates&quot;
RDSK=&quot;c0t2d0s5&quot;
RDSK=&quot;c0t2d0s0 0&quot;

# Get old Information
ALL_OLD=`awk -F&quot; &quot; &quot;/$RDSK/&quot;'{print $3,$4,$5,$6,$7}' dumpdates `;echo $ALL_OLD
#DAY_OLD=`awk -F&quot; &quot; &quot;/$RDSK/&quot;'{print $3}' dumpdates`;echo $DAY_OLD
#MONTH_OLD=`awk -F&quot; &quot; &quot;/$RDSK/&quot;'{print $4}' dumpdates`;echo $MONTH_OLD
#DATE_OLD=`awk -F&quot; &quot; &quot;/$RDSK/&quot;'{print $5}' dumpdates`;echo $DATE_OLD
#TIME_OLD=`awk -F&quot; &quot; &quot;/$RDSK/&quot;'{print $6}' dumpdates`;echo $TIME_OLD
#YEAR_OLD=`awk -F&quot; &quot; &quot;$RDSK/&quot;'{print $7}' dumpdates`;echo $YEAR_OLD

#Get New Date
MONTH_NEW=`date`

# check the variables
echo &quot;New date is $MONTH_NEW&quot;
echo &quot;Old date is $ALL_OLD&quot;

# Do actual edit
echo -n &quot;Preparing to edit $FILENAME &quot;
ex $FILENAME <<EOF
1
/c0t2d0s0*0
.s/$MONTH_NEW/$ALL_OLD/
wq
EOF

echo &quot;...Done&quot;


exit 0
 
ex $FILENAME <<EOF
/c0t2d0s0.*0/s/${MONTH_NEW}/${ALL_OLD}/
wq
EOF

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Try this:
Code:
.s/$ALL_OLD/$MONTH_NEW/

Hope This Help
PH.
 
Thanks...Not exactly right but close enough for me to get it to work.

/${RDSK}/s/${ALL_OLD}/${MONTH_NEW}/

Now on to the next step....try it on the remote system.

THANKS PEOPLES
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top