Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

hard coding keyboard input in a Bash script

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
TonyLB
LXF regular


Joined: Tue Apr 12, 2005 8:08 pm
Posts: 112
Location: Wirral, UK

PostPosted: Tue Jan 10, 2006 2:45 pm    Post subject: hard coding keyboard input in a Bash script Reply with quote

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
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8453
Location: Warrington, UK

PostPosted: Tue Jan 10, 2006 2:58 pm    Post subject: RE: hard coding keyboard input in a Bash script Reply with quote

Code:
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)
Back to top
View user's profile Send private message
CJLL
LXF regular


Joined: Sat Jul 09, 2005 10:22 pm
Posts: 193

PostPosted: Tue Jan 10, 2006 11:03 pm    Post subject: RE: hard coding keyboard input in a Bash script Reply with quote

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

Code:

#!/bin/sh

echo "Param 1: $1"

update-alternatives --config $1



Alternatively if you want your script to get interactive

Code:

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

update-alternatives --config $NAME

Back to top
View user's profile Send private message
TonyLB
LXF regular


Joined: Tue Apr 12, 2005 8:08 pm
Posts: 112
Location: Wirral, UK

PostPosted: Wed Jan 11, 2006 3:40 pm    Post subject: RE: hard coding keyboard input in a Bash script Reply with quote

Thanks folks - I should have thought of echo!

Neltz's one does just what I needed.

Tony
Back to top
View user's profile Send private message
drws
LXF regular


Joined: Sat Aug 06, 2005 11:39 am
Posts: 125

PostPosted: Wed Jan 11, 2006 6:22 pm    Post subject: Re: RE: hard coding keyboard input in a Bash script Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast