1

I have the Linux script shown below. I can get it to return from the method decrypt nothing which I need to unzip a file. The method decrypt sends a string with the name of a zip file. Please give some advice. I mention that another methods it brings correctly the files.

m_mode_verbose=1
const_1="ceva"
val="valoare"



decrypt ()
{

PASSPHRASE="xxxx"

encrypted=$1
local decrypt1=`echo $encrypted | awk '{print substr($0,0,64)}'`

echo "$PASSPHRASE"|gpg --no-tty --batch --passphrase-fd 0 --quiet --yes --decrypt -o ${sspTransferDir}/${decrypt1} ${sspTransferDir}/${encrypted} 2> /dev/null
if [ $? -eq 0 ]
then
notify "pgp decrypt of file.pgp succeeded"
else
notify "pgp decrypt of file.pgp failed"
fi


#   PASSPHRASE=”your passphrase used for PGP”
#   echo "$PASSPHRASE"|gpg --no-tty --batch --passphras
#e-fd 0 --quiet --yes \
#–decrypt -o file.dat file.pgp 2> /dev/null
#if [ $? -eq 0 ]
#then
#        echo "pgp decrypt of file.pgp succeeded"
#else
#        echo "pgp decrypt of file.pgp failed"
#fi
# echo "testtest $decrypt1"
echo "valoare ="$decrypt1


val=$decrypt1
#eval $decrypt1
$CONST=$decrypt1
echo "local"$CONST
}

process_file()
{
f=$1
echo "Processing $f"
for encrypted in `cat $f`; do
        echo "Name of the file: "$i
        echo "Decrypted : " $decrypted
        decrypted=$(decrypt ${encrypted})   #decrypted = decrypt(encrypted)
         # decrypted=decrypt ${encrypted} ${decrypted}  #decrypted = decrypt(encrypted)
        echo "val ============== " $val
      echo "Decrypted after method" $decrypted
    unzip -o  ${TransferDir}/${decrypted} -d  ${ImportRoot}
        echo "Path after unzip" $ImportRoot
        #rm -f ${decrypted}
        echo "After remove" $decrypted
        path=${sspTransferDir}/${encrypted}
        #rm -f ${sspTransferDir}/${encrypted}
        echo "Path to remove" $path
        echo "Const ="$CONST
done

}


#main


get_config;
file="output$lang.txt"
echo "file is $file"
get_file_list $file # fills $file with the list of encrypted files corresponding to language $language
process_file $file  #process - decrypt,
Thomas Dickey
  • 76,765
SocketM
  • 113

2 Answers2

2

Shell functions imitate subprocesses; like subprocesses, their return value is only an 8-bit number, conventionally indicating success (0) or failure (nonzero). To pass data out of a function, store it in a variable. Variables are not local to a function unless declared to be so.

decrypt () {
  …
  valoare="$decrypt1"
}

…
decrypt
decrypted="$valoare"

Note that I haven't reviewed your script. It's hard to read with the broken indentation and variable names that don't seem to relate to what they're used for. I do see some obvious potential problems: many commands are missing double quotes around variable substitutions. Always put double quotes around variable and command substitutions: "$val", etc. There are also other bits that don't make sense, like $CONST=$decrypt1 — to set the variable CONST, remove the $.

1

To answer the title of your question, shell functions usually return data by printing it to stdout. Callers capture the return value with retval="$(func "$arg1" "$arg2" "$@")" or similar. The alternative is to pass it the name of a variable to store a value in (with printf -v "$destvar").

If your script isn't working, it might be from quoting problems. You're missing quotes on a lot of variable expansions.

e.g.

echo "valoare ="$decrypt1
# should be:
echo "valoare =$decrypt1"

Your version quotes the literal part, but then leaves the user-data open for interpretation by the shell. Multiple whitespace characters in $decrypt1 collapse to a single space in echo's output.

Peter Cordes
  • 6,466