Linux Blog

Shell Script to get user input

Filed under: Shell Script Sundays — Owen at 2:06 am on Sunday, January 27, 2008

Creating a shell script to get input is rather easy. Shell scripts prompting input are generally more user friendly too. In this article I’ll show you how to read input from the bash shell. Take the example below:

#!/bin/bash

echo "Shell Script To Get User Input";

while read inputline
do
what="$inputline"
echo $what;

if [ -z "${what}" ];
then
exit
fi

done

All it does is echo’s the introduction “Shell Script To Get User Input” and then goes right to the bash read input loop. The next line makes a variable so that we can echo it out and also check if its empty with the if [ -z part. If the script is empty we exit, if not we loop around another time.

This is a very basic example but it can easily be modified so that you can use bash to grab user input. If you have any trouble with this script drop me a comment and I’ll be happy to help you out.

Man Pages for commands in this post »

echo
echo

19 Comments »

248

Comment by Nicole Freeman

January 27, 2008 @ 9:48 pm

Hi,

I’m trying to compile a boot script that accepts user input on the HOSTNAME variable (SUSE Linux during the boot process, puts this user-provided hostname in the /etc/HOSTNAME file, and reboots the system. Can you plz help me with this? I’m new to scripting and was hoping to find some help on this…

TIA,
-Nicole

251

Comment by Owen

January 28, 2008 @ 10:20 am

So, you are trying to change the host name by user input on boot up? Are you saying after user input it puts it in the hosts file but reboots instead of booting up?

255

Comment by Nicole Freeman

January 29, 2008 @ 1:17 am

Correct, the inital hostname would be assigned randomly by DHCP. I was hoping to run this script postinstall so that the user was free to choose the hostname, and a reboot would “cleanly” set it by adding it to the /etc/HOSTNAME file.

Thanks much!!!

258

Comment by Owen

January 29, 2008 @ 11:13 am

I’m not familiar with SuSe but in some other distributions there are network configuration scripts. Maybe you could try to strace that program and see what it is doing. Check out http://www.thelinuxblog.com/uploads/netconfig
It creates temp files for all of the files needed and if that is correct I believe it copies them. If you need any more help let me know.

269

Comment by Nicole Freeman

January 31, 2008 @ 11:48 am

Thanks Owen, I’ll look at this script and see how I can use it to get my job done!
Appreciate your help on this.

270

Comment by Owen

January 31, 2008 @ 11:58 pm

Anytime :) Let me know if you need any more help and feel free to comment on any posts with questions

282

Comment by Nicole Freeman

February 4, 2008 @ 11:55 am

Hi Owen,

Yaay! I was able to use portions of the netconfig script to copy the host and domainname during system startup. It worked extremely well!

I was also requested to however (in the same startup script), provide a couple of screens to add a user to the system, and have the user type his/her password while running a bg sanity check to ensure that the userid is not a duplicate.

I tried drafting this part on my own and it looked awful :-( Could you help me with this please?

TIA,
Nicole

290

Comment by Owen

February 5, 2008 @ 11:32 am

I’ll look into it.

adduser is a command, you might be able to use expect if your PC has it. useradd does require a unique ID.

294

Comment by Nicole Freeman

February 5, 2008 @ 5:05 pm

Hi Owen,

The SLES version I’m using only has the “useradd” binary, which I guess is good from what you just mentioned wrt its internal duplicate check process.

Meanwhile this is the portion of netconfig I’ve used in my boot script. The last line of the script really did it for me.

Thank you so much, and I’m really sorry for the trouble!

-Nicole

# Main loop:
while [ 0 ]; do
cat < $TMP/tempmsg
First, we’ll need the name you’d like to give your host.
Only the base hostname is needed right now. (not the domain)

Enter hostname:
EOF
dialog –title “ENTER HOSTNAME” –inputbox “`cat $TMP/tempmsg`” 11 65 \
$HOSTNM 2> $TMP/SeThost
if [ $? = 1 -o $? = 255 ]; then
rm -f $TMP/SeThost $TMP/tempmsg
exit
fi
HOSTNM=”`cat $TMP/SeThost`”
rm -f $TMP/SeThost $TMP/tempmsg
if [ ! “$HOSTNM” = “” ]; then
break;
fi
done

while [ 0 ]; do
cat < $TMP/tempmsg
Now, we need the domain name for this machine, such as:

example.org

Do not supply a leading ‘.’

Enter domain name for $HOSTNM:
EOF
dialog –title “ENTER DOMAINNAME FOR ‘$HOSTNM’” –inputbox \
“`cat $TMP/tempmsg`” \
14 64 $DOMAIN 2> $TMP/SeTdom
if [ $? = 1 -o $? = 255 ]; then
rm -f $TMP/SeTdom $TMP/tempmsg
exit
fi
DOMAIN=”`cat $TMP/SeTdom`”
rm -f $TMP/SeTdom $TMP/tempmsg
if [ ! “$DOMAIN” = “” ]; then
break;
fi
done

echo $HOSTNM.$DOMAIN > etc/HOSTNAME

295

Comment by Owen

February 5, 2008 @ 5:16 pm

Glad that worked out for you.

I don’t think that useradd has a duplicate ID script.
It just so happens that adduser is also a Slackware script. Maybe you could use part of this too.

Its no trouble at all this is what The Linux Blog is here for :) Just make sure you drop by from time to time and leave me more comments :D

299

Comment by Nicole Freeman

February 7, 2008 @ 2:51 pm

Hi Owen,

Thanks for the adduser script.

I now have both pieces of code and wish to have them execute automatically when a “guest” user logs in.
The goal of this entire exercise is that each user will be given a laptop and a guest account with no password.

Once they login as “guest” they should be able to set a hostname of their choice (using portions of the netconfig script); and also be able to create their own userid’s.

I gave the “guest” user sudo perms (with NOPASSWD) on these scripts and put the absolute paths to these scripts in the .bashrc file of the “guest” user, but the GUI wouldn’t come up when I tried logging in as the guest user.

I know I’m doing something fundamentally wrong here. Once this is done; I should be all set.

Thank you.
-Nicole

300

Comment by Owen

February 7, 2008 @ 3:00 pm

What is your default run level?

Also, once we get this figured out can we write a blog post about it?

I’m not familiar with SLED but I don’t think that it needs any special user settings to startx.

Can you post the rest of the script?

- Owen.

301

Comment by Nicole Freeman

February 7, 2008 @ 4:50 pm

The default run level is 5. The GUI login screen comes up but what I mean is that, the script needs to be specified in a “bash-friendly” manner as it won’t bring up the guest users login screen but would simply show a blank screen after entering the username/password at the GUI login screen. So I’m guessing it has to do with how its specified in .bashrc. We can surely blog this once we’re done.

Thanks!

302

Comment by Owen

February 7, 2008 @ 5:13 pm

All right, Maybe change the run level to multi user no X. This time see if once its done you can just run the command startx.

370

Comment by Phani

February 28, 2008 @ 12:55 am

Hi Owen

I am trying to export the user input to a variable. I did used the example provided by you, but the loop is never ending. I want to stop the loop once the user has entered his input, so that I can export that input to another variable which can be used for other script.

Kindly help by any suggestion in achieving this task.

Thanks,
Phani.

371

Comment by Owen

February 28, 2008 @ 1:20 pm

Hi Phani,

This should work:

while read inputline
do
what=”$inputline”
done

echo $what

Hope this helps, Owen.

372

Comment by Phani

February 28, 2008 @ 6:49 pm

Thanks Owen
But its not working, the loop is not ending and the input is not passing to the variable.

Any help is much appreciated.

Thanks,
Phani.

373

Comment by Owen

February 29, 2008 @ 11:44 am

Phani,

Try this script: input.sh It should exit the loop with no problem.

739

Pingback by The Linux Blog » tweeting from the command line

June 29, 2008 @ 12:25 pm

[…] If you’d like more information on how this works read this article: Shell Script to get user input […]

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>