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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-07-2009, 08:23 PM
|
#1
|
Member
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190
Rep:
|
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
|
|
|
01-07-2009, 08:51 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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?
|
|
|
01-07-2009, 09:01 PM
|
#3
|
Member
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190
Original Poster
Rep:
|
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
|
|
|
01-07-2009, 10:13 PM
|
#4
|
Member
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190
Original Poster
Rep:
|
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
|
|
|
01-07-2009, 10:47 PM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
"${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"
|
|
|
01-07-2009, 10:53 PM
|
#6
|
Member
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190
Original Poster
Rep:
|
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
|
|
|
01-08-2009, 03:25 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by faizlo
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!
|
|
|
All times are GMT -5. The time now is 01:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|