pass a password to an application

Hi folks

I am new to shellscripting and I try to automate & guify some jobs.
Suppose I have a variable the stores a passwd and an application like "gpg" or "zip" to run from withn the script.

how do I pass that passwd (the content of the variable) to the application being called?

eg.

#/bin/bash
passwd=$1
gpg -d ~/thegpgfile.gpg

running this my terminal sits there waiting for me to enter a passwd. How do I automaticaly use the $passwd so the script continues execution without me interacting?

thank you in advance

p.s. excuse the english. its not me native language

For better control, you may want to try out "Expect" scripting - Expect - Expect - Home Page

Whereas a kludge using just shell scripts is as follows:

$ 
$ file testfile.*
testfile.gpg: GPG encrypted data
$ 
$ cat decrypt.sh
#!/bin/bash
gpg --output testfile.txt --decrypt testfile.gpg

$ 
$ cat passphrase.txt
disneyland
$ 
$ . decrypt.sh <passphrase.txt

You need a passphrase to unlock the secret key for
user: "Alice"
2048-bit ELG-E key, ID 46972146, created 2009-07-18 (main key ID 53FD32A9)

gpg: encrypted with 2048-bit ELG-E key, ID 46972146, created 2009-07-18
      "Alice"
$ 
$ file testfile.*
testfile.gpg: GPG encrypted data
testfile.txt: ASCII text
$ 
$ cat testfile.txt
this is a test file
that I'd like to encrypt/decrypt
using gpg
$ 
$ 

tyler_durden

As for GPG, there's been a lengthy thread here, with a lot of explanation.

I real dont want to use expect because of portability issues.
No offense...but I realy dont undersatnd your example...I see what you are doing...but what are you trying to say?

for now I am using this line:

`echo 'thepassword | gpg --passphrase-fd 0 -d thefile.gpg`

but that only gets me around the gpg issue, but not the core of the question -> how to pass strings to a running peece of code?!

further question:

how do I put a file (its content whether text or binary) into a shell variable?
This right here is not working:

file=`echo 'thepassword | gpg --passphrase-fd 0 -d thefile.gpg`
file $file
FILETYPE=`file $file | cut -d " " -f 2`
echo "$FILETYPE"

souldn't file tell me something like this:

[UTF-8 Unicode text

There isn't one well defined way of passing a password to a program. Some you can echo to, others won't allow that (ssh for example). gpg only allows it with the --passphrase-fd option.

As for your example: the file utility works on files. There's no option to read from a pipe. Better save the content to a temporary file, run file on it, and then read the content back into a variable with cat (or any other mechanism your shell might provide)