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.
|
|
03-30-2010, 10:54 AM
|
#1
|
LQ Newbie
Registered: Mar 2010
Posts: 13
Rep:
|
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
echo "Please enter the name of the file you would like to change"
cnt=0
while true
do
read -r fplan
if [[ -f "$fplan" || -f ../"$fplan" ]]
then
echo "File exists"
break
else
echo "Try again"
fi
((cnt+=1))
if [[ $cnt -eq 5 ]]
then
echo "exceeded 5 tries. quitting. Do you know what you're doing?"
exit
fi
done
echo
echo "Please wait while the task completes"
echo "Patience is a virture, posess it if you can"
echo "Seldom found in women, never in a man"
sleep 5
while read -r LINE
do
if [[ "${LINE}" =~ "SystemDeclarationData" ]]
then
echo "$LINE"
read -r LINE
if [[ ${LINE} == 215656 ]] #inject 1
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_1/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215506 ]] || [[ ${LINE} == 215509 ]] || [[ ${LINE} == 215575 ]] #inject 12
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_12/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 216199 ]] #inject 36
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_36/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 216288 ]] #inject 20
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_20/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 214900 ]] #inject 44
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_44/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 214910 ]] || [[ ${LINE} == 215031 ]] || [[ ${LINE} == 215032 ]] || [[ ${LINE} == 215033 ]] || [[ ${LINE} == 215034 ]] || [[ ${LINE} == 215035 ]] || [[ ${LINE} == 215036 ]] || [[ ${LINE} == 215037 ]] || [[ ${LINE} == 215038 ]] || [[ ${LINE} == 215039 ]] || [[ ${LINE} == 215040 ]] || [[ ${LINE} == 215041 ]] || [[ ${LINE} == 215042 ]] || [[ ${LINE} == 215098 ]] || [[ ${LINE} == 215099 ]] || [[ ${LINE} == 215184 ]] || [[ ${LINE} == 215185 ]] || [[ ${LINE} == 215186 ]] || [[ ${LINE} == 215187 ]] || [[ ${LINE} == 215188 ]] || [[ ${LINE} == 215245 ]] || [[ ${LINE} == 215322 ]] || [[ ${LINE} == 215323 ]] || [[ ${LINE} == 215324 ]] || [[ ${LINE} == 215325 ]] || [[ ${LINE} == 215326 ]] || [[ ${LINE} == 215327 ]] || [[ ${LINE} == 215328 ]] || [[ ${LINE} == 215329 ]] || [[ ${LINE} == 215330 ]] || [[ ${LINE} == 215331 ]] || [[ ${LINE} == 215332 ]] || [[ ${LINE} == 215333 ]] || [[ ${LINE} == 215334 ]] || [[ ${LINE} == 215390 ]] || [[ ${LINE} == 215391 ]] || [[ ${LINE} == 215468 ]] || [[ ${LINE} == 215469 ]] || [[ ${LINE} == 215470 ]] || [[ ${LINE} == 215471 ]] || [[ ${LINE} == 215472 ]] || [[ ${LINE} == 215473 ]] || [[ ${LINE} == 215474 ]] || [[ ${LINE} == 215475 ]] || [[ ${LINE} == 215476 ]] || [[ ${LINE} == 215477 ]] || [[ ${LINE} == 215478 ]] || [[ ${LINE} == 215479 ]] || [[ ${LINE} == 215480 ]] || [[ ${LINE} == 215536 ]] || [[ ${LINE} == 215614 ]] || [[ ${LINE} == 215615 ]] || [[ ${LINE} == 215616 ]] || [[ ${LINE} == 215617 ]] || [[ ${LINE} == 215766 ]] || [[ ${LINE} == 215767 ]] || [[ ${LINE} == 215768 ]] || [[ ${LINE} == 215769 ]] || [[ ${LINE} == 215770 ]] || [[ ${LINE} == 215771 ]] || [[ ${LINE} == 215828 ]] || [[ ${LINE} == 215829 ]] || [[ ${LINE} == 215203 ]] || [[ ${LINE} == 215204 ]] || [[ ${LINE} == 215205 ]] || [[ ${LINE} == 215206 ]] || [[ ${LINE} == 215207 ]] || [[ ${LINE} == 215208 ]] || [[ ${LINE} == 215209 ]] || [[ ${LINE} == 215210 ]] || [[ ${LINE} == 215211 ]] || [[ ${LINE} == 215219 ]] || [[ ${LINE} == 215343 ]] || [[ ${LINE} == 215345 ]] || [[ ${LINE} == 215351 ]] || [[ ${LINE} == 215352 ]] || [[ ${LINE} == 215353 ]] || [[ ${LINE} == 215354 ]] || [[ ${LINE} == 215355 ]] || [[ ${LINE} == 215356 ]] || [[ ${LINE} == 215357 ]] || [[ ${LINE} == 215358 ]] || [[ ${LINE} == 215359 ]] || [[ ${LINE} == 215360 ]] || [[ ${LINE} == 215361 ]] || [[ ${LINE} == 215362 ]] || [[ ${LINE} == 215363 ]] || [[ ${LINE} == 215365 ]] || [[ ${LINE} == 215392 ]] || [[ ${LINE} == 215429 ]] || [[ ${LINE} == 215489 ]] || [[ ${LINE} == 215492 ]] || [[ ${LINE} == 215493 ]] || [[ ${LINE} == 215176 ]] || [[ ${LINE} == 215177 ]] || [[ ${LINE} == 215178 ]] || [[ ${LINE} == 215179 ]] || [[ ${LINE} == 214951 ]] || [[ ${LINE} == 215221 ]] || [[ ${LINE} == 215660 ]] || [[ ${LINE} == 215951 ]] || [[ ${LINE} == 215043 ]] || [[ ${LINE} == 215044 ]] || [[ ${LINE} == 215045 ]] || [[ ${LINE} == 215046 ]] || [[ ${LINE} == 215047 ]] || [[ ${LINE} == 215048 ]] || [[ ${LINE} == 215049 ]] || [[ ${LINE} == 215050 ]] || [[ ${LINE} == 215100 ]] || [[ ${LINE} == 215101 ]] || [[ ${LINE} == 215190 ]] || [[ ${LINE} == 216067 ]] || [[ ${LINE} == 216068 ]] || [[ ${LINE} == 214901 ]] || [[ ${LINE} == 214902 ]] || [[ ${LINE} == 214903 ]] || [[ ${LINE} == 214904 ]] || [[ ${LINE} == 214905 ]] || [[ ${LINE} == 214906 ]] || [[ ${LINE} == 214923 ]] || [[ ${LINE} == 214924 ]] || [[ ${LINE} == 214925 ]] || [[ ${LINE} == 214926 ]] || [[ ${LINE} == 214927 ]] || [[ ${LINE} == 214928 ]] || [[ ${LINE} == 214929 ]] || [[ ${LINE} == 214930 ]] || [[ ${LINE} == 214931 ]] || [[ ${LINE} == 214932 ]] || [[ ${LINE} == 214933 ]] || [[ ${LINE} == 214934 ]]|| [[ ${LINE} == 214935 ]] || [[ ${LINE} == 214936 ]] || [[ ${LINE} == 214937 ]] || [[ ${LINE} == 214938 ]] || [[ ${LINE} == 214939 ]] || [[ ${LINE} == 214940 ]] || [[ ${LINE} == 214941 ]] || [[ ${LINE} == 214971 ]] || [[ ${LINE} == 214972 ]] || [[ ${LINE} == 215109 ]] || [[ ${LINE} == 215110 ]] || [[ ${LINE} == 215112 ]] || [[ ${LINE} == 215113 ]] || [[ ${LINE} == 215114 ]] || [[ ${LINE} == 215115 ]] || [[ ${LINE} == 215116 ]] #inject 35
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 216214 ]] #inject 46
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_46/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215776 ]] #inject 46_1
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_46_1/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215919 ]] #inject 46_2
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_46_2/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215923 ]] #inject 46_3
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_46_3/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 216272 ]] || [[ ${LINE} == 216287 ]] #inject 46_4
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_46_4/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215620 ]] || [[ ${LINE} == 215621 ]] || [[ ${LINE} == 215622 ]] || [[ ${LINE} == 215623 ]] || [[ ${LINE} == 215624 ]] || [[ ${LINE} == 215625 ]] || [[ ${LINE} == 215682 ]] || [[ ${LINE} == 215683 ]] || [[ ${LINE} == 215760 ]] || [[ ${LINE} == 215905 ]] || [[ ${LINE} == 215906 ]] || [[ ${LINE} == 215907 ]] || [[ ${LINE} == 215908 ]] || [[ ${LINE} == 215909 ]] || [[ ${LINE} == 215910 ]] || [[ ${LINE} == 215916 ]] || [[ ${LINE} == 215917 ]] || [[ ${LINE} == 215974 ]] || [[ ${LINE} == 215975 ]] || [[ ${LINE} == 216051 ]] || [[ ${LINE} == 216052 ]] || [[ ${LINE} == 216053 ]] || [[ ${LINE} == 216054 ]] || [[ ${LINE} == 216055 ]] || [[ ${LINE} == 216056 ]] || [[ ${LINE} == 216057 ]] || [[ ${LINE} == 216058 ]] || [[ ${LINE} == 216059 ]] || [[ ${LINE} == 216060 ]] || [[ ${LINE} == 216061 ]] || [[ ${LINE} == 216062 ]] || [[ ${LINE} == 216063 ]] || [[ ${LINE} == 216120 ]] || [[ ${LINE} == 216121 ]] || [[ ${LINE} == 216197 ]] || [[ ${LINE} == 216198 ]] || [[ ${LINE} == 216200 ]] || [[ ${LINE} == 216201 ]] || [[ ${LINE} == 216202 ]] || [[ ${LINE} == 216203 ]] || [[ ${LINE} == 216204 ]] || [[ ${LINE} == 216205 ]] || [[ ${LINE} == 216206 ]] || [[ ${LINE} == 216207 ]] || [[ ${LINE} == 216208 ]] || [[ ${LINE} == 216209 ]] || [[ ${LINE} == 216273 ]] || [[ ${LINE} == 216282 ]] || [[ ${LINE} == 216283 ]] #inject 10_4
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_10_4/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215193 ]] || [[ ${LINE} == 215194 ]] || [[ ${LINE} == 215195 ]] || [[ ${LINE} == 215196 ]] || [[ ${LINE} == 215246 ]] || [[ ${LINE} == 215247 ]] || [[ ${LINE} == 215335 ]] || [[ ${LINE} == 215336 ]] || [[ ${LINE} == 215337 ]] || [[ ${LINE} == 215338 ]] || [[ ${LINE} == 215339 ]] || [[ ${LINE} == 215340 ]] || [[ ${LINE} == 215341 ]] || [[ ${LINE} == 215342 ]] || [[ ${LINE} == 215393 ]] || [[ ${LINE} == 215481 ]] || [[ ${LINE} == 215482 ]] || [[ ${LINE} == 215483 ]] || [[ ${LINE} == 215484 ]] || [[ ${LINE} == 215485 ]] || [[ ${LINE} == 215486 ]] || [[ ${LINE} == 215487 ]] || [[ ${LINE} == 215488 ]] || [[ ${LINE} == 215539 ]] || [[ ${LINE} == 215626 ]] || [[ ${LINE} == 215627 ]] || [[ ${LINE} == 215628 ]] || [[ ${LINE} == 215629 ]] || [[ ${LINE} == 215630 ]] || [[ ${LINE} == 215631 ]] || [[ ${LINE} == 215632 ]] || [[ ${LINE} == 215331 ]] || [[ ${LINE} == 215332 ]] || [[ ${LINE} == 215633 ]] || [[ ${LINE} == 215634 ]] || [[ ${LINE} == 215685 ]] || [[ ${LINE} == 215772 ]] || [[ ${LINE} == 215773 ]] || [[ ${LINE} == 215774 ]] || [[ ${LINE} == 215775 ]] || [[ ${LINE} == 215777 ]] || [[ ${LINE} == 215778 ]] || [[ ${LINE} == 215779 ]] || [[ ${LINE} == 215780 ]] || [[ ${LINE} == 215831 ]] || [[ ${LINE} == 215918 ]] || [[ ${LINE} == 215920 ]] || [[ ${LINE} == 215921 ]] || [[ ${LINE} == 215922 ]] || [[ ${LINE} == 216069 ]] || [[ ${LINE} == 216070 ]] || [[ ${LINE} == 216071 ]] || [[ ${LINE} == 216072 ]] || [[ ${LINE} == 216123 ]] || [[ ${LINE} == 216210 ]] || [[ ${LINE} == 216211 ]] || [[ ${LINE} == 216212 ]] || [[ ${LINE} == 216213 ]] || [[ ${LINE} == 216215 ]] || [[ ${LINE} == 216216 ]] || [[ ${LINE} == 216217 ]] || [[ ${LINE} == 216218 ]] || [[ ${LINE} == 216265 ]] || [[ ${LINE} == 216267 ]] || [[ ${LINE} == 216274 ]] || [[ ${LINE} == 216275 ]] || [[ ${LINE} == 216280 ]] || [[ ${LINE} == 216281 ]] || [[ ${LINE} == 216287 ]] #inject 10_2
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_10_2/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215537 ]] #inject 44_2
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_44_2/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215346 ]] || [[ ${LINE} == 215347 ]] || [[ ${LINE} == 215348 ]] || [[ ${LINE} == 215349 ]] || [[ ${LINE} == 215350 ]] || [[ ${LINE} == 215494 ]] || [[ ${LINE} == 215495 ]] || [[ ${LINE} == 215496 ]] || [[ ${LINE} == 215497 ]] || [[ ${LINE} == 215498 ]] || [[ ${LINE} == 215499 ]] || [[ ${LINE} == 215500 ]] || [[ ${LINE} == 215501 ]] || [[ ${LINE} == 215502 ]] || [[ ${LINE} == 215503 ]] || [[ ${LINE} == 215504 ]] || [[ ${LINE} == 215505 ]] || [[ ${LINE} == 215507 ]] || [[ ${LINE} == 215508 ]] || [[ ${LINE} == 215510 ]] || [[ ${LINE} == 215538 ]] || [[ ${LINE} == 215635 ]] || [[ ${LINE} == 215637 ]] || [[ ${LINE} == 215638 ]] || [[ ${LINE} == 215639 ]] || [[ ${LINE} == 215640 ]] || [[ ${LINE} == 215641 ]] || [[ ${LINE} == 215642 ]] || [[ ${LINE} == 215643 ]] || [[ ${LINE} == 215644 ]] || [[ ${LINE} == 215645 ]] || [[ ${LINE} == 215646 ]] || [[ ${LINE} == 215647 ]] || [[ ${LINE} == 215648 ]] || [[ ${LINE} == 215649 ]] || [[ ${LINE} == 215650 ]] || [[ ${LINE} == 215651 ]] || [[ ${LINE} == 215652 ]] || [[ ${LINE} == 215653 ]] || [[ ${LINE} == 215654 ]] || [[ ${LINE} == 215655 ]] || [[ ${LINE} == 215684 ]] || [[ ${LINE} == 215721 ]] || [[ ${LINE} == 215781 ]] || [[ ${LINE} == 215783 ]] || [[ ${LINE} == 215784 ]] || [[ ${LINE} == 215785 ]] || [[ ${LINE} == 215786 ]] || [[ ${LINE} == 215787 ]] || [[ ${LINE} == 215788 ]] || [[ ${LINE} == 215789 ]] || [[ ${LINE} == 215790 ]] || [[ ${LINE} == 215791 ]] || [[ ${LINE} == 215792 ]] || [[ ${LINE} == 215793 ]] || [[ ${LINE} == 215794 ]] || [[ ${LINE} == 215795 ]] || [[ ${LINE} == 215796 ]] || [[ ${LINE} == 215797 ]] || [[ ${LINE} == 215798 ]] || [[ ${LINE} == 215799 ]] || [[ ${LINE} == 215800 ]] || [[ ${LINE} == 215801 ]] || [[ ${LINE} == 215802 ]] || [[ ${LINE} == 215830 ]] || [[ ${LINE} == 215867 ]] || [[ ${LINE} == 215927 ]] || [[ ${LINE} == 215929 ]] || [[ ${LINE} == 215930 ]] || [[ ${LINE} == 215931 ]] || [[ ${LINE} == 215932 ]] || [[ ${LINE} == 215933 ]] || [[ ${LINE} == 215936 ]] || [[ ${LINE} == 215937 ]] || [[ ${LINE} == 215938 ]] || [[ ${LINE} == 215939 ]] || [[ ${LINE} == 215940 ]] || [[ ${LINE} == 215941 ]] || [[ ${LINE} == 215942 ]] || [[ ${LINE} == 215943 ]] || [[ ${LINE} == 215944 ]] || [[ ${LINE} == 215945 ]] || [[ ${LINE} == 215946 ]] || [[ ${LINE} == 215947 ]] || [[ ${LINE} == 215948 ]] || [[ ${LINE} == 215976 ]] || [[ ${LINE} == 216013 ]] || [[ ${LINE} == 216073 ]] || [[ ${LINE} == 216075 ]] || [[ ${LINE} == 216076 ]] || [[ ${LINE} == 216077 ]] || [[ ${LINE} == 216078 ]] || [[ ${LINE} == 216079 ]] || [[ ${LINE} == 216080 ]] || [[ ${LINE} == 216081 ]] || [[ ${LINE} == 216082 ]] || [[ ${LINE} == 216083 ]] || [[ ${LINE} == 216084 ]] || [[ ${LINE} == 216085 ]] || [[ ${LINE} == 216086 ]] || [[ ${LINE} == 216087 ]] || [[ ${LINE} == 216088 ]] || [[ ${LINE} == 216089 ]] || [[ ${LINE} == 216090 ]] || [[ ${LINE} == 216091 ]] || [[ ${LINE} == 216092 ]] || [[ ${LINE} == 216093 ]] || [[ ${LINE} == 216094 ]] || [[ ${LINE} == 216122 ]] || [[ ${LINE} == 216159 ]]
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_10_3/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215074 ]] || [[ ${LINE} == 215075 ]] || [[ ${LINE} == 215097 ]] || [[ ${LINE} == 215220 ]] || [[ ${LINE} == 215389 ]] || [[ ${LINE} == 215511 ]] || [[ ${LINE} == 215512 ]] || [[ ${LINE} == 215514 ]] || [[ ${LINE} == 215657 ]] || [[ ${LINE} == 215659 ]] || [[ ${LINE} == 215803 ]] || [[ ${LINE} == 215804 ]] || [[ ${LINE} == 215805 ]] || [[ ${LINE} == 215827 ]] || [[ ${LINE} == 215949 ]] || [[ ${LINE} == 215950 ]] || [[ ${LINE} == 215973 ]] || [[ ${LINE} == 216095 ]] || [[ ${LINE} == 216096 ]] || [[ ${LINE} == 216097 ]] || [[ ${LINE} == 216119 ]] || [[ ${LINE} == 216241 ]] || [[ ${LINE} == 216242 ]] || [[ ${LINE} == 216269 ]]
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_10_1/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215244 ]] #inject 35_1
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_1/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215111 ]] #inject 35_2
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_2/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215491 ]] #inject 35_3
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_3/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215189 ]] #inject 35_4
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_4/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215364 ]] #inject 35_5
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_5/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
elif [[ ${LINE} == 215283 ]] #inject 35_6
then
echo "$LINE"
count=0
while read -r LINE
do
if [[ $count == 23 ]]
then
echo "http://192.168.9.58/webpage/inject_35_6/firstframe.htm"
break
else echo "$LINE"
((count+=1))
fi
done
else echo "$LINE"
fi
else echo "$LINE"
fi
done < "$fplan" > $PWD/complete
sleep 5
echo
echo
echo "backing up original file"
cp "$fplan" "$fplan".bak
sleep 5
echo
echo "renaming the changed file"
mv $PWD/complete "$fplan"
sleep 3
echo
echo "operation completed"
echo
echo
|
|
|
03-30-2010, 01:23 PM
|
#2
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep:
|
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.
|
|
|
03-30-2010, 02:07 PM
|
#3
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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
215656 )
<commands>
;;
215506 | 215509 | 215575 )
<commands>
;;
...
esac
There's no need for the { and } in, for example [[ ${LINE} == 215074 ]]
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
do
read -r fplan
if [[ -f "$fplan" || -f ../"$fplan" ]]; then
echo "File exists"
break
else
echo "Try again"
fi
((cnt+=1))
if [[ $cnt -eq 5 ]]; then
echo "exceeded 5 tries. quitting. Do you know what you're doing?"
exit
fi
done
((cnt+=1)) could be ((cnt++)). Personally I use let cnt++, thinking it more legible.
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 '
Please wait while the task completes
Patience is a virture, posess it if you can
Seldom found in women, never in a man'
It's a good idea to check the output and return code of commands to ensure they worked and to stop processing if they did not. Error messages are conventionally written to stderr (this helps if you only want to check error messages). Combining both notions:
Code:
out="$( mv $PWD/complete "$fplan" 2>&1 )"
rc=$?
if [[ $rc -ne 0 || $buf ]]; then
echo "Problem moving file. Return code: $rc, stdout and stderr: '$out'" >&2
exit 1
fi
|
|
1 members found this post helpful.
|
04-02-2010, 12:27 PM
|
#4
|
LQ Newbie
Registered: Mar 2010
Posts: 13
Original Poster
Rep:
|
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?
rc=$?
if [[ $rc -ne 0 || $buf ]]; then ##is $buf a variable that is inherent to bash? from what i've learned so far the || means or. so what is $buf?
echo "Problem moving file. Return code: $rc, stdout and stderr: '$out'" >&2 #last question: what exactly is the 2>&1 and >$2 doing?
exit 1
fi
thanks again for the help and ideas. i'm having fun learning bash, and this community seems pretty great so far.
|
|
|
04-02-2010, 01:26 PM
|
#5
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by aSingularity
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?
# Yes
rc=$?
if [[ $rc -ne 0 || $buf ]]; then
##is $buf a variable that is inherent to bash?
# No
# what is $buf?
# The name is short for buffer, a usage based on Kernighan and Ritchie's work (good pedigree!), essentially meaning a temporary storage area.
# Only useful with buf="$( ...
# If you prefer $out to $buf, you could use if [[ $rc -ne 0 || $out ]]
# (empty strings are "true", others are false)
echo "Problem moving file. Return code: $rc, stdout and stderr: '$out'" >&2
#last question: what exactly is the 2>&1 and >$2 doing?
# 2>&1 says "send output for file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout).
# The out="$( has already directed stdout to $out.
# a) >$2 says send stdout to stderr.
# b) In this case it sends echo's output to stderr.
# c) It is conventional and useful to write error messages to stderr.
exit 1
fi
|
Regards the 15 functions to (as best I understand it) write configuration files based on configuration files, it sounds excessive but have a go anyway (most of us learn best by doing) then post here and we'll see if it can be simplified. Glad you're enjoying the adventure
Last edited by catkin; 04-02-2010 at 01:29 PM.
Reason: Formatting and clarity
|
|
|
04-02-2010, 01:51 PM
|
#6
|
LQ Newbie
Registered: Mar 2010
Posts: 13
Original Poster
Rep:
|
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!
|
|
|
04-02-2010, 02:01 PM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by aSingularity
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!
|
Purely personal preference; it's useful to have conventions for variable names (reduces need for personal memory, consistent usage makes scripts easier to understand ...) and $buf is what I use for short-lived "scratch" storage (inspired by aforementioned software giants). Usually its scope is made local to a function using the local builtin.
|
|
|
04-03-2010, 02:41 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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 11:48 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
|
|