You are not logged in.

#1 2009-09-05 18:21:11

zhurai
Member
From: somewhere
Registered: 2009-09-03
Posts: 15
Website

command line input

...ok, maybe my subject wasn't too good, but say I have two files

blah.sh and another random program that runs blah.sh

blah.sh
----------
#! /bin/bash

read $1
print $1
----------
(or something like that)

and the other program to run 'blah.sh' and then input (while running) the program something for blah.sh to read?

Offline

#2 2009-09-05 18:29:40

Wintervenom
Member
Registered: 2008-08-20
Posts: 1,011

Re: command line input

#!/bin/bash
#main.sh

#[...]
user_input=$(blah.sh)  #I'm assuming "blah.sh" has +x.
#[...]
#!/bin/bash
#blah.sh

read user_input
echo "$user_input"

Last edited by Wintervenom (2009-09-05 18:33:38)

Offline

#3 2009-09-05 19:32:24

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: command line input

If you mean you want blah.sh to read input from the user and return it to your main script, Wintervenom's suggestion is good.  If you want to pass input to blah.sh from inside your script (so you edit it in your main script and it shows on blah.sh's stdin), use:

/path/to/blah.sh <<EOF
This is input that will be passed to blah.sh.
Everything up to the EOF is captured and passed to the script.
EOF

Offline

#4 2009-09-05 19:53:16

zhurai
Member
From: somewhere
Registered: 2009-09-03
Posts: 15
Website

Re: command line input

erm...

like

there is
"blah.sh" and "main.sh"

main.sh has some things to pass to blah.sh

./main.sh
would then run ./blah.sh or whatever, and then input, for example "123" (without the user running the program to input it)
and then blah.sh would output 123

Offline

#5 2009-09-05 21:33:41

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: command line input

Maybe you mean something like this?

main.sh

blah.sh Hello World

blah.sh

echo $*

Offline

#6 2009-09-05 21:55:11

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: command line input

zhurai wrote:

...ok, maybe my subject wasn't too good

Yeah, not so good. Please use descriptive thread titles so that others may be able to benefit from this thread.

Offline

#7 2009-09-06 12:09:58

zowki
Member
From: Trapped in The Matrix
Registered: 2008-11-27
Posts: 582
Website

Re: command line input

Instead of using examples like blah.sh and main.sh can you give us exactly what you want to accomplish? There may be a better way to do the job.


How's my programming? Call 1-800-DEV-NULL

Offline

#8 2009-09-06 20:46:53

zhurai
Member
From: somewhere
Registered: 2009-09-03
Posts: 15
Website

Re: command line input

skottish wrote:
zhurai wrote:

...ok, maybe my subject wasn't too good

Yeah, not so good. Please use descriptive thread titles so that others may be able to benefit from this thread.

not sure exactly what to call it `-`

HashBox wrote:

Maybe you mean something like this?

main.sh

blah.sh Hello World

blah.sh

echo $*

tested it, but...not really =_=

zowki wrote:

Instead of using examples like blah.sh and main.sh can you give us exactly what you want to accomplish? There may be a better way to do the job.

well...really I was thinking of if it was possible... but it's like:

main.sh:
#! /bin/bash
./two.sh
#???
#???
#???

and

two.sh:
#!/bin/bash
read $1
print $1


how exactly would I get main.sh to input something like "Hello World" or whatever message into the read (as in, putting in *that* information w/o user input)

or however you say it ~_~

Offline

#9 2009-09-06 21:08:24

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: command line input

Maybe like this then?

main.sh

blah.sh Hello World

blah.sh

USERINPUT="$*"
print $USERINPUT

Or MUST it be using "read"?

Offline

#10 2009-09-06 21:18:41

zhurai
Member
From: somewhere
Registered: 2009-09-03
Posts: 15
Website

Re: command line input

HashBox wrote:

Maybe like this then?

main.sh

blah.sh Hello World

blah.sh

USERINPUT="$*"
print $USERINPUT

Or MUST it be using "read"?

well, read/Scanner (for java)/<STDIN> (for perl)/cin (for C++)/etc

~_~

Last edited by zhurai (2009-09-06 21:19:16)

Offline

#11 2009-09-07 02:01:48

zhurai
Member
From: somewhere
Registered: 2009-09-03
Posts: 15
Website

Re: command line input

oh, right..   one of the reasons I wanted to do this is because ssh requires a password, but isn't in the command line arguments from what I can see, thus I have to type it in every time/etc....

Offline

#12 2009-09-07 03:39:55

Pox
Member
From: Melbourne, AU
Registered: 2007-10-04
Posts: 66

Re: command line input

I'd recommend using ssh keys if you want passwordless login, but for what you describe Trent's solution seems right: you can feed stdinput to a script with

echo '123456' | blah.sh

Also - have a read about 'expect'.

Offline

Board footer

Powered by FluxBB