Looking for experience to make a script more efficient.
I'm new to bash scripting. I wrote this script to attach url's to specified 6 digit numbers in a configuration text file. My original goal was to be able to be able to pull the url's and the 6 digit numbers from .csv files. that would allow me to make the script more versatile, not only for this particular project, but also for other projects in regards to the configuration file. This script works, and has served it's purpose, but it is not very pretty, and it's probably not very efficient. What can I do to improve it and possibly make it more versatile. I've thought about functions and arrays, but my skill set is still pretty limited. I'm not looking for someone to write it for me, just to point me in the right direction. Thanks in advance!
Code:
#!/bin/bash |
Biggest change I can see that would be beneficial is subroutines/functions, give it a try and if you're having problems come back here and let us know what they are and we'll give ya a hand.
|
Using a case-esac statement would save a lot of "elif"s and make it clearer that the only variable being tested was $LINE.
Code:
case $LINE in It would be easier to read and debug if code within do-done and if-else-fi was indented so the "do"s lined up with the "done"s. Putting "then"s after the tests would make the code more compact and facilitate if-else-fi alignment Code:
while true A single echo can write several lines. Using single quotes makes it obvious that the string is to be printed verbatim, without any substitutions Code:
echo ' Code:
out="$( mv $PWD/complete "$fplan" 2>&1 )" |
fellas, this is good stuff. I appreciate you getting back to me so quick. I've been reading through the advanced bash scripting guide. Case seems like a pretty handy command to have in the repertoire. From what I've been reading it seems pretty versatile. I'm not going to have an opportunity to try it until after the holiday, but I think what i'd like to do is write a function that would load a particular set of numbers into an array based on other criteria that is in the .csv file. This would require approx 15 separate functions, but would encompass all of the relevant numbers. I could then use a case statement to write them to the proper spot in the config file, count the 23 lines down and write the url. I think that this would also allow me to make the whole deal more useful. The config file that i am writing to is for a simulation game. Each line in the file corresponds to certain types of data in respect to any given entity. If I were to use variables for various data types, e.g. entity name, affiliation and whatnot, then the script would become a lot more useful and would save a lot of time during the build process. I apologize for this not so descriptive reply, I haven't quite thought it all out yet.
I do have a couple of questions though: Code:
out="$( mv $PWD/complete "$fplan" 2>&1 )" ## would this replace the move command that is currently in the script? |
Quote:
|
I understand. Is there a benefit to using $buf as compared to $out? or is it more of a preference type thing? Thanks for the explanation!
|
Quote:
|
Would you be able to provide the input and desired output? Might have some other suggestions on improvements.
|
All times are GMT -5. The time now is 12:14 PM. |