Linux Blog

Shell Script Flow Control – my most refered to articles

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:38 pm on Sunday, November 9, 2008

This week I do not have time to write a full fledged article on shell scripting, so I am going to some flow control and logic articles that I wrote that I read the most.

One script that I continuously refer to is one of the first shell scripting articles I wrote titled “When Photoshop Fails.” The reason I refer to this article is because it describes a couple of looping techniques and a loop that works with spaces in filenames. For those interested but do not want to read the whole thing here is the loop in short:

find * -iname “*” | while read i; do echo “$i”; done

The next article I’m linking to is the one I wrote on loops: http://www.thelinuxblog.com/for-while-and-until-loops-in-bash/ it outlines some of the basic looping techniques. What I fail to mention in this article is that seq can be used to generate sequences of numbers for the [in list] section. The following is how you would create a loop to loop from 1 to 10 echoing out each number:

for i in `seq 1 10`; do echo $i; done;

Select Statements in Bash is exactly what the title implies. How to implement select statements in bash scripting. Also known as switch’s and case’s in other languages many will be familiar with this sort of logic. Even though I do not refer to it as often as some of the other articles I think it is worth of a mention in this list.

Decision making using if statements is something that every shell scripter is bound to come across. While not exactly the most in depth article on bash if then else statements it is a good start for any one wanting to learn more.

I love dialog’s and it just so happens that dialog and xdialog do a great job of making dialogs for your shell scripts. Creating Dialogs with Dialog has some basic uses of dialog. While it is not exactly logic or flow control, it can be used to prompt the user for input or just to pretty things up a little. It just so happens that it has an X front end called xdialog which does a good job too.

This is not all of the articles I refer to, but its the ones I wrote. If any one else has a list of articles they refer to that are bookmarked that I wrote or not I’d be interested to see them. Just paste them in a comment.

Reworking Shell Scripts – Part 2

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:00 pm on Sunday, September 14, 2008

In the last Shell Script Sunday’s article I wrote, I said that I’d show you some more ways to rework shell scripts to make them easier to use. I’ve got some more tricks up my sleeve that I’d like to share, itsĀ  been rather busy this site of the internet at TheLinuxBlog.com. So why I write up some more shell scripting methods, here are some previous posts that can enhance your shell scripts. Be sure to comment on them if you find them useful, or would like more information.

Creating Dialogs with Dialog
If you have a shell script that you use on a regular basis, you may want to consider using dialog to make it more user friendly. Dialog makes it easy to create easy to use dialogs that are intuitive and easy to use. There are so many combinations of dialogs that can be created that the possibilities are ended. Dynamically create dialogs for select lists, input boxes, progress bars and much much more.

Graphical Shell Scripting
This article I wrote introduces graphical shell scripting. Similar to dialog this is an updated “Dialog” and works within X. If you support end users, or your target audience is Ubuntu/Linspire users (j/k) then XDialog may be the better choice. Its got most of the same functionality as Dialog except it depends on X. You can even support both Dialog and XDialog as they pretty much use the same syntax.

Changing Window Manager on Fedora

Filed under: General Linux,Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 8:34 am on Monday, August 18, 2008

TheLinuxBlog.com was intended to be a blog where I could log my thoughts, findings and generally keep track of how to do stuff. Since it was started this is still the goal. Ultimately I’d like to refer to TheLinuxBlog on how to do something just as much as I refer to google for everything else. That being said, one thing that recently came up for me was “How do I change my desktop on Fedora”.

I had done this before but I couldn’t remember the command to do so. Well, since this blog is as much for me as it is for the reader I figure I can post the how to here and kill two birds out with one blog post, I mean stone.

The program I use to change my window manager on Fedora is: switchdesk.

Switchdesk can be installed by Yum or if you installed Fedora from DVD or CD and didn’t fine tune your packages then you probably have it already. All you have to do to run it is type:

switchdesk

Now, if you are in an X session you will get a nice graphical dialog that will help you change your desktop manager. If your at the terminal it will exit and ask you nicely to type either gnome, kde, xfce or any other window manager you may have installed.

Don’t ask me why every distribution has a different named command and interface to achieve the same thing thats just the way it is. Maybe one day I’ll get a list of all of the commands and post them. Alternatively if anyone wants to start a list feel free to post them in comments or by e-mail.

Managing Services on Fedora

Filed under: Linux Software — TheLinuxBlog.com at 12:01 am on Wednesday, July 2, 2008

Fedora is a great desktop system, it has a pretty good services manager called service. It can be used to turn on, off and restart most services that run on the system. Should you want to manage running services all you have to do is type:

service <service name> <stop | start | restart>

If you don’t know the service name it can normally be found by issuing the –status-all switch. Doing so will output a long list of services so you may want to grep it or use a pager such as less:

[root@linux-blog ~]# service –status-all | less

[root@linux-blog ~]# service –status-all | grep snmp
capi not installed – No such file or directory (2)
JAVA_EXECUTABLE or HSQLDB_JAR_PATH in ‘/etc/sysconfig/hsqldb’ is set to a non-file.
snmpd is stopped
snmptrapd is stopped

While service is a great utility to manage services it does not stop them from running at startup. To manage services that start up when you boot you can use a handy little dialog script called “ntsysv”:

[root@linux-blog ~]# ntsysv

This will give you a nice dialog which is intuitive and similar to those of text based installers. Select the services you want to start up and then tab over to OK. I turn a lot of services off that I don’t use, if you’re unsure of what you need, try stopping it with the services command first and see if anything breaks. If after test it works well just turn it off.

Since I don’t use SELinux on my development machine, I always turn off setroubleshootd. I used ntsysv to stop it from starting at bootup and if I need it I can use the service command to start it.

Here is a screenshot of ntsysv in action:

managing services with ntsysv on Fedora

Making your scripts user and sysadmin friendly

Filed under: Shell Script Sundays — TheLinuxBlog.com at 11:08 pm on Sunday, November 18, 2007

When designing a shell script it is important to make them easy to use but also to make it easily automated for deployment. One example of this that comes to mind is the NVIDIA installer. It has command line options to allow for deployment but also gives a nice interface for the end user.

To implement this “dialog” can be used for the user interface and “getopts” can be used for the command line options. The script may look something like:

#help function
help () {
echo “Linux Blog – getopts and dialog example”;
echo “Usage:”;
echo -e “\t -h shows this help”;
echo -e “\t -a [y/n][other] ANSWER (Yes, No or Other)”;
}

#show dialog to get the answers
showDialog () {
dialog –yesno “Do you want to enter y?” 5 50 && \
ANS=”Yes was entered using dialog” ||\
ANS=”No was entered using dialog”
showAnswer;
}

#actually show the answer
showAnswer() {
echo $ANS;
}

#check answer for command line processing
checkAns() {
if [ "${OPT1}" == "y" ]
then
ANS=”Yes sent by getopts”;
elif [ "${OPT1}" == "n" ]
then
ANS=”No was sent getopts”;
else
ANS=”This: $OPT1 was sent by getopts”;
fi
#call showAnswer
showAnswer;
}

#get the options
while getopts “a:h” ARG;
do case “${ARG}” in
a) OPT1=”${OPTARG}”;;
h) HELP=”TRUE”;;
esac;
done

#see if help was entered
if [ "${HELP}" ]
then
#display help and quit
help;
exit;
fi
#if the options are empty
if [ -z "${OPT1}" ]
then
showDialog;
else
checkAns;
fi

Keep this getopts and dialog post in mind next time your shell scripting. It will take a little extra time to implement but the result will be a user and sysadmin friendly script.