question about sed and other stuff


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question about sed and other stuff
# 1  
Old 12-31-2005
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
# 2  
Old 12-31-2005
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
# 3  
Old 01-01-2006
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

# 4  
Old 01-01-2006
Quote:
sed -e \"s/$word/$word2/\" $file > $tmpd/$tfile
you do n't need a \ before "
# 5  
Old 01-01-2006
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 10:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

More Arduino Stuff...

HI all... (Apologies for any typos.) To add to Neo's Arduino subject matter I have decided to upload this in ".zip" format. Ignore "*.info" files these are AMIGA icons only and also the "HAM" drawer as these are photos in ancient AMIGA HAM modes. I have noticed that there are current... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. Shell Programming and Scripting

awk stuff

Hi, My input file data will be |ABCD|EFGH|IJKL|MNOP |ABCD|EF\|GH|IJKL|MNOP I am expecting output , |"ABCD"|"EFGH"|"IJKL"|"MNOP" |"ABCD"|"EF|GH"|"IJKL"|"MNOP" Note : The change basically the pipe deilmited file does contain | as value for some of the column but | will come with... (8 Replies)
Discussion started by: Nandy
8 Replies

3. Shell Programming and Scripting

Please help with monitoring stuff

Hi, I am trying to write a script to do monitoring kind of stuff, requirement - when a server is given a start it updates a file called server.log, I need to keep on grepping the word "Running" and as soon as it comes , script should be exited with the message , "Server came up... (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

4. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

5. Windows & DOS: Issues & Discussions

weird stuff

I coudln't think of another topic to post this under as the OS on the system is XP pro. Ok here is the go. I'm upgrdaing a mates computer. A AMD 1200Mhz and well it wouldn't boot from the CD to do a fresh install (By upgrade I mean OS with complete new install). So I opened up the box and... (4 Replies)
Discussion started by: woofie
4 Replies

6. UNIX for Dummies Questions & Answers

Simple stuff.

I hacked my TIVO a few months ago. I made a computer specifically for this, but I only used a UNIX boot disk to get all the TIVO goodies to work. I am intersted in getting some version of UNIX on this machine and getting it onto my network. I only want to do some simple file transfers, maybe... (1 Reply)
Discussion started by: IamJAWA
1 Replies
Login or Register to Ask a Question