| View previous topic :: View next topic |
| Author |
Message |
TonyLB LXF regular
Joined: Tue Apr 12, 2005 8:08 pm Posts: 111 Location: Wirral, UK
|
Posted: Tue Jan 10, 2006 2:45 pm Post subject: hard coding keyboard input in a Bash script |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7999 Location: Warrington, UK
|
Posted: Tue Jan 10, 2006 2:58 pm Post subject: RE: hard coding keyboard input in a Bash script |
|
|
| 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. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
CJLL LXF regular
Joined: Sat Jul 09, 2005 10:22 pm Posts: 193
|
Posted: Tue Jan 10, 2006 11:03 pm Post subject: RE: hard coding keyboard input in a Bash script |
|
|
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 |
|
 |
TonyLB LXF regular
Joined: Tue Apr 12, 2005 8:08 pm Posts: 111 Location: Wirral, UK
|
Posted: Wed Jan 11, 2006 3:40 pm Post subject: RE: hard coding keyboard input in a Bash script |
|
|
Thanks folks - I should have thought of echo!
Neltz's one does just what I needed.
Tony |
|
| Back to top |
|
 |
drws LXF regular
Joined: Sat Aug 06, 2005 11:39 am Posts: 125
|
Posted: Wed Jan 11, 2006 6:22 pm Post subject: Re: RE: hard coding keyboard input in a Bash script |
|
|
| 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 previous topic :: View next topic |
|