The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-31-2005
Registered User
 

Join Date: Dec 2005
Posts: 13
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
its ugly but its my first.

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
or
Code:
ftproot off
Any help is apreceated

slim
Reply With Quote
Forum Sponsor
  #2  
Old 12-31-2005
Registered User
 

Join Date: Dec 2005
Posts: 13
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
I guess I should read and search more posts before asking questions...

thanks
Reply With Quote
  #3  
Old 12-31-2005
Registered User
 

Join Date: Dec 2005
Posts: 13
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
results in this
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
Reply With Quote
  #4  
Old 12-31-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Quote:
sed -e \"s/$word/$word2/\" $file > $tmpd/$tfile
you do n't need a \ before "
Reply With Quote
  #5  
Old 01-01-2006
Registered User
 

Join Date: Dec 2005
Posts: 13
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
one thing I see when debugging is this:
Code:
-bash-2.05b$ froot dont

+ '[' dont == on ']'
+ '[' dont == off ']'

or
-bash-2.05b$ froot on

+ '[' off == on ']'
+ '[' off == off ']'
Could some one explain this please?

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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0