LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2004, 12:03 PM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
bash script question


here is the script i have right now:
It replaces Feb with April in lzwBackup.c, without actually opening the file up and retyping it.
######################################
# !/bin/sh
# gerbSed.sh

sed 's/Feb/April/' lzwBackup.c > lzw
cat lzw > lzwBackup.c
echo done!
######################################
i want ot be able to replace anytype of string to any file.

can i use command arguments, like so:

./gerbSed orgString newString FileName. and it will replace the old string with the new string into FileName.

i'm new to bash programing.

Last edited by djgerbavore; 07-08-2004 at 12:05 PM.
 
Old 07-08-2004, 01:02 PM   #2
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
Passing shell variables to sed is strange I usually will make a 2 part script. The first part will prepare the real script and the second script actually is what you want to run.

My sample script that will do something close:

##################
script_location=/tmp/junk_script_to_run
echo Preparing sed script...
echo "sed 's/$1/$2/g' $3" > $script_location
###
#insert what ever other thing you want like your temp file to save to and copy back to here
###
chmod 755 $script_location
echo "Script in $script_location will replace $1 with $2 in file: $3"
echo Run $script_location to launch this script.
echo "complete"
##################

There is probably a more elagant way to do it but this might get you a little further along.
 
Old 07-08-2004, 01:25 PM   #3
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Blinker_Fluid
Passing shell variables to sed is strange I usually will make a 2 part script. The first part will prepare the real script and the second script actually is what you want to run.

My sample script that will do something close:

##################
script_location=/tmp/junk_script_to_run
echo Preparing sed script...
echo "sed 's/$1/$2/g' $3" > $script_location
###
#insert what ever other thing you want like your temp file to save to and copy back to here
###
chmod 755 $script_location
echo "Script in $script_location will replace $1 with $2 in file: $3"
echo Run $script_location to launch this script.
echo "complete"
##################

There is probably a more elagant way to do it but this might get you a little further along.
i'm a little confused with this, what is junk_script_to_run? and how does this script work? Sorry, i'm just started to learn shell programing. Thank for your help
 
Old 07-08-2004, 03:39 PM   #4
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
junk_script_to_run is a file that was created by the first script. It contains the actual script that would do the work. It's duct tape and baling wire in the linux world. I could have just made the first script include the second like so:

BLINK--> cat sample_script
#this script takes 3 inputs
# first input string to replace
# second string replacement string
# third filename
script_location=/tmp/junk_script_to_run
echo Preparing sed script...
echo "sed 's/$1/$2/g' $3"
echo "sed 's/$1/$2/g' $3" > $script_location
chmod 755 $script_location
echo "Script in $script_location will replace $1 with $2 in file: $3"
echo Run $script_location to launch this script.
echo "running the script now:"
$script_location


my sample file:
BLINK --> cat junk2
red green blue
red green orange
blue green green
green orange purple

when running my script:
BLINK --> ./sample_script green FOO junk2
Preparing sed script...
sed 's/green/FOO/g' junk2
Script in /tmp/junk_script_to_run will replace green with FOO in file: junk2
Run /tmp/junk_script_to_run to launch this script.
running the script now:
red FOO blue
red FOO orange
blue FOO FOO
FOO orange purple

As far as redirecting it back to itself that's just 2 more lines you had in your original script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script question drj000 Programming 11 03-07-2005 10:25 PM
Bash script question deiussum Programming 6 08-14-2004 11:43 AM
bash script question xscousr Programming 5 07-03-2003 05:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 02:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration