Add 5 lines of code to all the scripts in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add 5 lines of code to all the scripts in a directory
# 1  
Old 09-18-2009
Add 5 lines of code to all the scripts in a directory

Hi Guys,
I need some tips on writing a Korn shell script that would look for certain lines of code and replace all the scripts in the directory with a few other lines. I have about 120 scripts that I need to modify.

Any suggestions would be appreciated!

Thanks,
Cool_avi
# 2  
Old 09-18-2009
I dont have knowledge in Korn Shell Scripts.

But i believe you would find sed fit for it. After testing use the -i switch of it to make the changes in that file.
# 3  
Old 09-18-2009
Are you are doing one-line-for-one-line replacements? That is, are you replacing something like
cd $HOME/old
with
cd $HOME/new
?
# 4  
Old 09-18-2009
Hi Ken,
No, I want to replace 1 line with about 5 lines of code. Those 5 lines of code include an if loop.

Please let me know if you need any other information.

Thanks,
coolavi
# 5  
Old 09-18-2009
If you want to use sed, something like this will work.

It looks for the first line containing the word 'special' and replaces the whole line with a five-line 'if' statement.

Of course, I'm using GNU sed, which recognizes the -i switch. Not all versions of sed do. And I'm not sure all seds recognize the \n sequence.
Code:
#!/bin/sh
cd script-directory
for f in *; do
    test ! -x "$f"  &&  continue
    sed -i '/special/ s/^.*$/if \[ "$a" = 1 \]; then\necho "1"\necho "2"\necho "3"\nfi/;q' "$f"
done

# 6  
Old 09-21-2009
Thanks Ken!

I haven't tried the solution yet. I would like to look into sed's syntax and interpret what you did.

I will let you know if I have any questions.

Thanks,
Coolavi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Moving directory structure and scripts from HP to Solaris

Hi, I am presently working in a migration project from HP Unix to Sun Solaris. I need to place all the directory structures, shell scripts and users into Sun Solaris. By doing this task manually there is a possibility for discrepencies. So any tools are there to do these kind of... (4 Replies)
Discussion started by: nag_sathi
4 Replies

2. Shell Programming and Scripting

creating directory from scripts

Dear All, I have a shell scripts which create a directory and perform moving some files, when the script is kept where it is creating directory then it runs fine , but when the scripts is run where it is supposed to be which is different location then where i am creating directory , scripts... (2 Replies)
Discussion started by: guddu_12
2 Replies

3. Shell Programming and Scripting

Help for shell scripts for moving all files from one directory to another

Hi , Can you please check this code .I am getting the follwing error while executing the script. Please help me out. #rm /tmp/$$ #!/bin/ksh dir_one="/usr/bin/sou" dir_two="usr/bin/sou/temp" for files in $dir_one/*.txt do ... (31 Replies)
Discussion started by: soumyamishra
31 Replies

4. Shell Programming and Scripting

Counting lines of code in a directory with awk

I've never toyed with awk, but it seems every time I present an elegant 2- to 8-line script, someone comes back with an awk 1-liner. I just came up with this to count all the lines of source code in a directory. How would I do it in awk? LINES=0 for n in $(wc -l *.cpp *.h | cut -b-7); do ... (2 Replies)
Discussion started by: KenJackson
2 Replies

5. Shell Programming and Scripting

Start all scripts in a directory

Good morning. I have a tricky one here for me. Hope somebody can help. I am looking for a way to take a single script in a directory and use it to fire all scripts within a subdirectory. For example. Lets say I have the following in /lcl/prd/apps file1.sh file2.sh file3.sh file4.sh... (2 Replies)
Discussion started by: LRoberts
2 Replies

6. Shell Programming and Scripting

my scripts does not check if directory exists

Hello: Can someone please help me figure out what is wrong here, my script does not move on to the "else" part even though there is no .ssh directory on my remote server: $more putkey.sh #!/bin/ksh for server in `cat list` do if ; then cat $HOME/.ssh/id_rsa.pub |ssh $server ' cat >>... (4 Replies)
Discussion started by: Sara-sh
4 Replies

7. Shell Programming and Scripting

replacing new lines in all files of a directory containing old lines

Hi all, I am trying to replace a few lines with other lines of all files in a directory which contain those few lines. say - there are some 10 files in a dir having the same 4 lines as 1.txt at the starting 1.txt line 1 line 2 line 3 line 4 ....................................... (1 Reply)
Discussion started by: rooster005
1 Replies

8. UNIX for Dummies Questions & Answers

CRON job to execute all scripts in a directory

Hi everyone: I'm trying to make a CRON job that will execute Fridays at 7am. I have the following: * 7 * * 5 I've been studying up on CRON and I know to have this in a file and then "crontab filename.txt" to add it to the CRON job list. The CRON part I believe I understand, but I would... (6 Replies)
Discussion started by: Annorax
6 Replies

9. Shell Programming and Scripting

scripts in root directory

Is it good practice to leave scripts in the root directory? if no, why is it not? (1 Reply)
Discussion started by: stephen adebayo
1 Replies

10. Shell Programming and Scripting

get home-directory in shell-scripts

Hi, does somebody know, how i can get the full path of my home-directory in a shell-script? But not with the $home-variable or ~ (i don't know for sure, but i think this does not work in every shell...). Thank You! Greetings, qsi (5 Replies)
Discussion started by: qsi
5 Replies
Login or Register to Ask a Question