![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| weird stuff | woofie | Windows & DOS: Issues & Discussions | 4 | 11-16-2004 04:41 PM |
| /dev/hd* ?? & HTFS .... What is this stuff? | Cameron | UNIX for Dummies Questions & Answers | 1 | 07-03-2002 07:36 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
question about sed and other stuff
I'm just getting into shell scripting and have run into a problem.
I sometimes need to ftp in as root while I'm SShed in, rather than going to the ftp conf file all the time to allow root access, I thought I'd bone up on shell scripting to make it easier. I wrote a little script to do this. however I've run into some questions. the script works like it is here: I call it ftproot Code:
#!/bin/bash #work directory wdir="/root/test" #temp directory tmpd="/root/test/tmp" #original file file="myfile.txt" #word="RootLogin off" #word2="RootLogin on" #temp file tfile="myfile1.txt" cp $wdir/$file $tmpd sleep 1 sed -e 's/RootLogin off/RootLogin on/' $file > $tmpd/$tfile sleep 1 mv -f $tmpd/$tfile $wdir/$file ## clean the tmp dir rm -f $tmpd/$file But if I use the word and word2 variable in the sed statment, it breaks. I assume it's because of the space between RootLogin and on or off? is this right? If so can I parse out the space somehow in the variable so it can be used? also what method (function or case or ??) should I use to finish the script so all I have to do is Code:
ftproot on Code:
ftproot off slim |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Humm I found my answer in another post the varibles for the text in sed works if a take out the single quotes and replace them with double ones..
Code:
sed -e "s/$word/$word2/" $file > $tmpd/$tfile thanks |
|
#3
|
|||
|
|||
|
I seem to be running in circles any help would be nice
Code:
bash$rootftp on Code:
#!/bin/bash -x
#work directory
wdir="/home/web_users/test"
#temp directory
tmpd="tmp"
#original file
file="myfile"
word="RootLogin off"
word2="RootLogin on"
#temp file
tfile="myfile1.txt"
on=1
off=1
if [ $on = 1 ]; then
cp $wdir/$file $tmpd
sleep 1
sed -e \"s/$word/$word2/\" $file > $tmpd/$tfile
sleep 1
mv -f $tmpd/$tfile $wdir/$file
## clean the tmp dir
rm -f $tmpd/$file
elif [ $on = no ]; then
cp $wdir/$file $tmpd
sleep 1
sed -e \"s/$word2/$word/\" $file > $tmpd/$tfile
sleep 1
mv -f $tmpd/$tfile $wdir/$file
## clean the tmp dir
rm -f $tmpd/$file
else
echo "you must enter either on or off"
fi
done
Code:
-bash-2.05b$ ./on on + wdir=/home/httpd/web_users/test + tmpd=/home/httpd/web_users/test/tmp + file=myfile + word=RootLogin off + word2=RootLogin on + tfile=myfile1.txt + on=1 + '[' 1 = 1 ']' + cp /home/httpd/web_users/test/myfile tmp + sleep 1 + sed -e '"s/RootLogin' off/RootLogin 'on/"' myfile sed: -e expression #1, char 1: Unknown command: `"' + sleep 1 + mv -f /home/httpd/web_users/test/tmp/myfile1.txt /home/web_users/test/myfile + rm -f /home/httpd/web_users/test/tmp/myfile |
|
#4
|
||||
|
||||
|
Quote:
|
|
#5
|
|||
|
|||
|
yes.. thank you... good catch, I thought I read that all quotes within a command must be escaped...
Thanks again Here is a working model... any glaring errors? Code:
#!/bin/bash
#work directory
wdir="/home/httpd/web_users/test"
#temp directory
tmpd="/tmp"
#original file
file="myfile"
word="RootLogin off"
word2="RootLogin on"
#temp file
tfile="myfile1.txt"
if [ "$1" == on ]; then
cd $wdir
cp -f $file $tmpd
sed -e "s/$word/$word2/" $file > $tmpd/$tfile
sleep 1
mv -f $tmpd/$tfile $wdir/$file
rm -f $tmpd/$file
echo "Root Can Now FTP In"
elif [ "$1" == off ]; then
cd $wdir
cp -f $file $tmpd
sleep 1
sed -e "s/$word2/$word/" $file > $tmpd/$tfile
sleep 1
mv -f $tmpd/$tfile $wdir/$file
rm -f $tmpd/$file
echo "Root FTP Is Now Un-Available"
else
echo "You Must Do Either ./froot on or ./froot off"
fi
Code:
-bash-2.05b$ froot dont + '[' dont == on ']' + '[' dont == off ']' or -bash-2.05b$ froot on + '[' off == on ']' + '[' off == off ']' six hours to get this figured out to replace what? <1 min. with vi...lrh I like this stuff!! on another note, Should I give sed more time to do its work than 1 second.. my biggest fear is that the ftp.conf file will get munged if there is a hang or whatever... Thanks again Last edited by vbslim; 01-01-2006 at 07:06 AM. |
|||
| Google The UNIX and Linux Forums |