LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-question-202633/)

djgerbavore 07-08-2004 12:03 PM

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.

Blinker_Fluid 07-08-2004 01:02 PM

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.

djgerbavore 07-08-2004 01:25 PM

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

Blinker_Fluid 07-08-2004 03:39 PM

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.


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