Visit Our UNIX and Linux User Community


Scripting change of text in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting change of text in another file
# 1  
Old 08-06-2009
Scripting change of text in another file

Hello, I am pretty new to UNIX/bash scripting, so this question may seem obvious. My experience is simply stringing commands together in a script, maybe doing some if/then testing and such, so I haven't gotten into anything too heavy...

I have a shell script that I use as a template to create backup scripts for certain directories. These scripts are essentially the same, with the exception of the value of some variables. Other than that, they are identical. So, it is easy enough to simply make copies of this template, then manually go in and change the variable names (I make separate scripts for each directory, as some are deemed more important, so I want them backed up via cron more frequently). To make the copies, I have simply done:

for copy in dir1 dir2 dir3; do
cp template.sh $copy.sh
done

It is crude yes, but like I said, I am inexperienced (I'm trying though!). I was wondering what I would need in order to change the variable values as well, so I can script it all. That way, I could edit the template file, then run one script and it would produce the copies and change their variable values. I have done some reading on 'awk' and 'sed', but am not sure which I need (or if I need both)

Just a nudge is all I need... No need to do it for me! Thanks for any insight.
# 2  
Old 08-06-2009
Wouldn't it be easier to make the generic backup script ("template") accept parameters, then call the very same script at different times with different parameters, approximately ...

Cron tab

Code:
00 21 * * *  sh backup.sh dir1
00 22 * * *  sh backup.sh dir2
00 23 * * *  sh backup.sh dir3

Shell script

Code:
SOURCE="$1"
# [code here]

# 3  
Old 08-06-2009
Wow, that would be SO much easier. I had no idea that you could do that (I told you I was a rookie!). So, is it possible to use more that one argument? I would think so, just looking at some other commands I have used with many arguments, but...
# 4  
Old 08-06-2009
Sure. Parameter 1 is $1, 2 is $2, and so forth. $# is the number of parameters available. The 'shift' command makes it easy to put it in a loop by getting rid of the first parameter and moving them all down. This would print all parameters, first to last:
Code:
while [ "$#" -gt 0 ]
do
        echo "Parameter is $1"
        shift
done

Parameters in functions work the exact same way as parameters on the command-line.
# 5  
Old 08-06-2009
Awesome. So I can essentially, write one script, only changing the arguments in the crontab, and all will be well. I am going to love the simplicity in this. Thanks for the help!
# 6  
Old 08-27-2009
Hello again, I am visiting this again, after running into a tiny snag... When using the directory as an argument in the crontab, it behaves completely erratically if the argument has a space in the name (which makes sense as instead of both words being used as an argument, one is being used as the first argument and the other is being used as the second).
I have tried using "misc info", 'misc info', misc\ info, and none seem to work. Should I enclose the name in {} or []? Maybe combine using quotes and escaping the space with a \?

Just to clarify...

My crontab looks vaguely like this:

x x x x x /path/to/script.sh Misc Info enc

So, the script is looking for two arguments, but seeing three (Misc, Info, and enc). I have been trying all sorts of crazy combinations to get Misc Info recognized as one argument with no luck...

Last edited by vwgtiturbo; 08-27-2009 at 08:30 PM..
# 7  
Old 08-29-2009
Enclosing both the argument and it's "interpretation" with single and double quotes, respectively, should do the trick - approximately (no Penguin within reach to ask for confirmation, sorry ...):

Script Call

Code:
sh backup.sh 'dir1'

Script Code

Code:
SOURCE="$1"


Previous Thread | Next Thread
Test Your Knowledge in Computers #944
Difficulty: Medium
The original smurf.c was written by Dan Moschuk, aka TBone.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below test-000000 test-000001 #and so on until 14 now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text? sample... (5 Replies)
Discussion started by: Sekhar419
5 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. Shell Programming and Scripting

Shell scripting:from text file to CSV

Hello friends, I have a file as follows: "empty line" content1 content2 content3 content1 content2 content3 content1 content2 content3 It starts with an empty line, how can i get a csv like this: (12 Replies)
Discussion started by: kraterions
12 Replies

4. Shell Programming and Scripting

Change the text in 2nd column in file

Legends, I have following entries in a file. I want to toggle the entry of 2nd column. TRUE/FALSE i tried using perl -p -i -e but the problem is, if instead of "tab" space after 1st column, there is a character or multi character space then this won't work. perl -p -i -e... (6 Replies)
Discussion started by: sdosanjh
6 Replies

5. Shell Programming and Scripting

Scripting Help needed with a text file.

Hi, Iam a novice to unix shell scripting, need a help from you guys.The scenario is as follows, i have the following Text file. order No Company Category 21 aaa A 24 aaa A 87 aaa B 98 aaa B 23 abc A 45 abc B 25 bbb A 76 wes A 66 ... (6 Replies)
Discussion started by: tech_frk
6 Replies

6. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

7. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

8. Shell Programming and Scripting

change text color in postscript file

Hi everyone, I have a program that produces postscript files on the fly The color of the files produced are in black and white I want to change the text color of postscript file as the file is being produced without having to manually go into the ps file and change it myself. Any Ideas?... (5 Replies)
Discussion started by: walforum
5 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies

Featured Tech Videos