ProgrammingThis 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.
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.
This is a "snippit" of a program I wrote. The program adds/deletes lines from a pipe delimited file. The code listed here is the part thats doing the deleting, and is designated with the -r. The logic is flow is as such: read the field number, Ask if its correct, then see if the field exists in the file. If it doesn't, report and error, and exit. If it does have the field, spit out some info on it, and delete. A sample fieldlist is provided just for clarification. It also needs to delete multiple lines at once.
I'm looking for any and all comments on ways to accomplish what I've done in less lines of code, or perhaps with a bit more style. I'm also looking to pick up a different language... perl perhaps. Any advice is appreciated.
Code:
FIELDLISTFILE="/home/dave/fieldlist.txt"
# <Sample fieldlist.txt> #
111|alpha|
222|bravo|
333|charlie|
444|gamma|
555|delta|
555|dupel|
# </Sample fieldlist.txt> #
if [ ${1} = "-r" ]; then
checked=0
while [ $checked = 0 ];
do
clear
echo -ne "\nField number of line to remove: "
read -n3 field
echo -e "\n\nYou have selected line "$field" for removal."
sleep 1
echo -en "\nIs this correct?"
read -es -n1 -p "(y/n)" keypress
if [ $keypress = "y" ]; then
clear
grep $field $FIELDLISTFILE > /dev/null
if [ $? = 0 ]; then
grep -n $field $FIELDLISTFILE > temp.tmp
while read line
do
lineCut=`echo $line |cut -d\: -f1`
fieldName=`echo $line |cut -d\| -f2`
echo -e '\E[30;42m'"\n\nRemoved field ""$fieldName"", field number ""$field"" from the fieldlist.\033[0m"
sed -i ${lineCut}d ${FIELDLISTFILE}
sleep 2
clear
done < temp.tmp
rm -f temp.tmp
checked=1
else
clear
echo -e '\E[31;40m'"\n\nFIELD NOT FOUND, EXITING.\033[0m"
sleep 3
exit 1
fi
fi
done
tput sgr0
fi
it's a useful tool.
you can use IFS to set the split char, if you just want to grab the
output not keep persistent variables it's safer to do in a subshell,
so you don't clobber IFS for other stuff, a'la:
Code:
# code here
(
IFS=:
while read name the_rest
do
echo $name
done < /etc/passwd
) | pipe_to_this
try this on the command line: (IFS=: && ls -1d $PATH)
certainly not, that's what the forum is for
I've done a fair amount of scripting in my time
though I'm actually a ksh lover!
here's a good one,
#!/bin/bash -eu
#!/bin/ksh -eu
I use a fair bit,
-e causes any error to exit the script immediately
-u causes any unbound variable to error, which is very useful for catching silly typos.
set -x
set -u
are the same thing but after the script starts
ahh, those are great to know. I'll post some code soon... I'm actually writing a bunch of stuff in KSH... but having major issues cron'ing these scripts.
I can't seem to get the darn thing to run correctly. Parts of the code work, but other parts just completely don't run. I know the script is working because I have a small logger function at the end... which does write to a log.
I think it has something to do with cron using SH for everything, but I'm perplexed at a way to work around it.
well, as long as the script has a #!/bin/ksh it will be fine.
the thing with cron is you start with a limited environment and
no default tty, for instance the $PATH variable is very limited.
you won't have all the stuff like $LOGNAME either.
you can of course source your .profile in your script
you can always test with a set -x also.
you can do limited cron job testing using batch though this inherits the current environment,
eg:
echo script | batch
Last edited by bigearsbilly; 06-25-2007 at 04:34 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.