hard coding keyboard input in a Bash script

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

hard coding keyboard input in a Bash script

Postby TonyLB » Tue Jan 10, 2006 2:45 pm

I'm trying to write a couple of Bash scripts which use utility programs that take keyboard input (eg update-alternatives --config xxx needs a choice from the keyboard.

I want to automate it from a parameter passed when the script is used. At the moment my best effort writes a file using the input parameter, runs update-alternatives redirecting input from the newly created file, then deletes the file.

There must be a better way I'm sure. How can you pass a parameter rather than use keyboard input without writing it to a file first?

Tony
TonyLB
LXF regular
 
Posts: 112
Joined: Tue Apr 12, 2005 7:08 pm
Location: Wirral, UK

RE: hard coding keyboard input in a Bash script

Postby nelz » Tue Jan 10, 2006 2:58 pm

Code: Select all
echo "xxx" | update-alternatives --config <&0


echo sends the command to stdout
the pipe sends the stdout to stdin for the next command
&0 is the file handle for stdin, so <&0 redirects it to the command.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: hard coding keyboard input in a Bash script

Postby CJLL » Tue Jan 10, 2006 11:03 pm

Bash scripts accept upto 9 parameters on the command line, they are named $1 to $9

Code: Select all
#!/bin/sh

echo "Param 1: $1"

update-alternatives --config $1



Alternatively if you want your script to get interactive

Code: Select all
!/bin/sh
echo -n "Enter your name: "
read -e NAME
echo "Hello $NAME"

update-alternatives --config $NAME

CJLL
LXF regular
 
Posts: 193
Joined: Sat Jul 09, 2005 9:22 pm

RE: hard coding keyboard input in a Bash script

Postby TonyLB » Wed Jan 11, 2006 3:40 pm

Thanks folks - I should have thought of echo!

Neltz's one does just what I needed.

Tony
TonyLB
LXF regular
 
Posts: 112
Joined: Tue Apr 12, 2005 7:08 pm
Location: Wirral, UK

Re: RE: hard coding keyboard input in a Bash script

Postby drws » Wed Jan 11, 2006 6:22 pm

CJLL wrote:Bash scripts accept upto 9 parameters on the command line, they are named $1 to $9


Don't forget ${10} ${11} ${12} etc. if you need more than 9 parameters
drws
LXF regular
 
Posts: 125
Joined: Sat Aug 06, 2005 10:39 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest