LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using BASH redirects and "prompt" files to interact with commands/scripts ?? (https://www.linuxquestions.org/questions/linux-newbie-8/using-bash-redirects-and-prompt-files-to-interact-with-commands-scripts-467317/)

swedish_lunacy 07-24-2006 11:34 PM

Using BASH redirects and "prompt" files to interact with commands/scripts ??
 
Hi there,
I am trying to interact with BASH commands/scripts using stdin redirects from a file but it doesn't seem to work on all commands

For example:

rm -Ri /some_dir < prompt_file

Seems to work

But the following mount command which prompts for a password:

mount -t cifs //other_computer/remote_dir /my_computer/local_dir/ < prompt_file

Does not seem to read the prompts from the redirected file. Instead it comes up with a prompt requesting for a password. ( PS I know that I can use the -o password="my_pass" option but I am trying to figure out what's going wrong with the redirects for this, I want to start using BASH scripts and redirects to interact with commands that are called )

Cheers
Nick

raskin 07-25-2006 01:13 AM

some commands check that the password is input from the terminal for security reasons. Try 'expect' to fool them..

konsolebox 07-25-2006 03:16 AM

hello there. the following might work for mount:

Code:

mount -t cifs //other_computer/remote_dir /my_computer/local_dir/ -p - < prompt_file
OR

Code:

cat promptfile | mount -t cifs //other_computer/remote_dir /my_computer/local_dir/ -p -
edit: what commands do you want to use btw?

swedish_lunacy 07-25-2006 03:21 AM

Raskin: Thanks for that, I will install expect on my system and have a play with it
konsolebox: Thanks, gave it a shot but it didn't seem to work. The man pages for mount show -p as expecting a file descriptor number for a passphrase. Is this something different ?

PS: When writing bash scripts is it always best to use $? to test for the return status of a recently run command or is there another way of gaining acces to a command's return code? ie is there a way to directly test a command's return code in the middle of a conditional statement

Code:

[ ( cat some_file | grep foo ) -eq 0 ] && echo "contains foo" || echo "does not contain foo"
rather than

Code:

cat some_file | grep foo
[ $? -eq 0 ] && echo "contains foo" || echo "does not contain foo"


konsolebox 07-25-2006 03:36 AM

Quote:

Originally Posted by swedish_lunacy
Code:

[ ( cat some_file | grep foo ) -eq 0 ] && echo "contains foo" || echo "does not contain foo"

should be
Code:

[ $(cat some_file | grep foo >/dev/null; echo $?) -eq 0 ] && echo "contains foo" || echo "does not contain foo"
also why not just
Code:

cat some_file | grep foo && echo "contains foo" || echo "does not contain foo"

swedish_lunacy 07-25-2006 03:43 AM

konsolebox,

Thanks for that, that's exactly what I was looking for :)

Thanks for your help, I'm still pretty new to shell scripting

:)

konsolebox 07-25-2006 03:48 AM

no problem. you can ask me more if you like. bash scripting's fun for me.

here's some good manuals too:

Advance Bash Scripting
http://www.tldp.org/LDP/abs/html/

Bash Guide for Beginners
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

swedish_lunacy 07-25-2006 04:12 AM

konsolebox,

Thanks for the links.

I can see why you enjoy bash scripting. It seems to me that the possibilities are endless ( once you get your head around the syntax ). I have come from an MS background and I am just stunned at how versatile linux is !

I have only just begun working on my first script. I'm generally pretty good at finding the information I need using a combination of TLDP, man, info, and google, but sometimes I can get pretty stuck on something simple and just need to be pointed in the right direction

If I end up getting stumped on something how can I get a hold of you ? Should I just post on the forum ?

Cheers
:p

raskin 07-25-2006 04:25 AM

There's no point to try to get answer from some particular forum member. You can always post (preferably after searching). Then members who are reading forum at the moment will try to help you. Yes, and don't forget 'info bash' while reading guides.. Most times man bash is sufficient.

konsolebox 07-25-2006 04:33 AM

Quote:

Originally Posted by swedish_lunacy
Thanks for the links.

you're welcome :)
Quote:

Originally Posted by swedish_lunacy
I have come from an MS background and I am just stunned at how versatile linux is !

me too. before when i was just using ms-dos i loved playing with the batch files. then transferred to win9x then to winnt's cmd.exe. i also make complicated scripts with cmd.exe.

by the time i went to linux, i was also stunned by the great powers of bash.
Quote:

Originally Posted by swedish_lunacy
If I end up getting stumped on something how can I get a hold of you ? Should I just post on the forum ?

just post a topic with bash in its title. like other members i'll just try to help too.
Quote:

Originally Posted by swedish_lunacy
Cheers
:p

cheers too

Edit: it really feels great to hear happy comments from the people you helped.

konsolebox 07-25-2006 04:35 AM

Quote:

Originally Posted by raskin
Most times man bash is sufficient.

i 2nd the motion :p

swedish_lunacy 07-25-2006 05:16 AM

Thanks guys. Will do

CroMagnon 07-25-2006 12:20 PM

CMD scripting is surprisingly capable, but for times when it's not enough, you may be interested in looking at Cygwin. Once you have become accustomed to the usefulness of bash (and the other GNU command line tools), it is a real boon to have them available on Windows as well.

konsolebox 07-25-2006 04:52 PM

comparing cmd to bash, cmd has one good command though.. the for loop. i like its token handling. but cmd still lacks many things so overall i'll still go for bash.

raskin 07-25-2006 05:10 PM

What does it do better than 'for' in bash, by the way?


All times are GMT -5. The time now is 11:35 PM.