LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2009, 08:23 PM   #1
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Rep: Reputation: Disabled
Better way to write this script


Hi All,

I have written the following script. I have just repeated some commands, and I am sure there is a more better way to do it. I hope I one of gurus here will help me make it in a better shape. Here is the script:

Code:
#! /bin/sh

sed -i -e "s/test2.xxx/test3.xxx/" -e "s/output2/output3/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test3.xxx/test4.xxx/" -e "s/output3/output4/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test4.xxx/test5.xxx/" -e "s/output4/output5/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test5.xxx/test6.xxx/" -e "s/output5/output6/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test6.xxx/test7.xxx/" -e "s/output6/output7/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test7.xxx/test8.xxx/" -e "s/output7/output8/" makeFV.C
root -l .x makeFV.C

sed -i -e "s/test8.xxx/test9.xxx/" -e "s/output8/output9/" makeFV.C
root -l .x makeFV.C
Thanks in advance,

faizlo
 
Old 01-07-2009, 08:51 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What about using a for loop over the sequence of numbers between 2 and 8 and use the arithmetic operator to retrieve the numbers between 3 and 9?
 
Old 01-07-2009, 09:01 PM   #3
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
Hi,
Thanks for the reply,

I got the first part of your answer (the use of a for loop), yet it is the second part that causes me the headache!
Also, I am not that good at programming

faizlo
 
Old 01-07-2009, 10:13 PM   #4
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
This is my first try:
Code:
#! /bin/sh

   for i in `seq 2 1 9`

     sed -i -e "s/test${i}/test${i+1}/" -e "s/output${i}/output${i+1}"
     root -l .x makeFV.C
done

echo "All is done well"
It did not work
I am using sed as you may have noticed, does this need special commands?

faizlo
 
Old 01-07-2009, 10:47 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"${i+1}" is not a mathematical operator. In this construct the + doesn't add anything, it specifies an alternate value in case the variable is null. See parameter substitution at the Advanced Bash Scripting Guide for more details. What you probably want is something like "test$(($i+1))". See arithmetic expansion for that.

Also, it's generally recommended that you use the $() form these days instead of backticks for command substitution. "for i in $(seq 2 1 9)". But actually in this case, seq probably isn't even needed. The for loop will automatically step through each number in the list anyway. "for i in 2 1 9"
 
Old 01-07-2009, 10:53 PM   #6
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
Thank you for the response, I will see what I can do about it.

It seems I need to read the abs guide, and I think I should

faizlo
 
Old 01-08-2009, 03:25 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by faizlo View Post
It seems I need to read the abs guide, and I think I should
This is the correct answer! Your question about shell programming is very basic, so I tried to encourage to look at the documentation. Anyway, in addition to what already explained by David the H, in bash you can use the extended brace expansion (available since bash version 3) to get a sequence of numbers. Then use the double parentheses construct to do calculations.

An example script to achieve what you're trying to do is
Code:
#!/bin/bash
for i in {2..8}
do
  sed -i -e "s/test$i.xxx/test$((i + 1)).xxx/" -e "s/output$i/output$((i + 1))/" makeFV.C
  root -l .x makeFV.C
done
Hope you will deepen into this. Cheers!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how write a script saif Linux - Newbie 8 07-27-2007 01:31 PM
script to write to a cd nasirdaudahmad Linux - Hardware 3 01-24-2007 12:45 AM
How to write a Script Blake Linux - Software 6 07-18-2004 12:41 PM
Help with a script I need to write... cmfarley19 Programming 9 12-06-2003 01:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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