简体   繁体   中英

Getting a Perl command output into a shell variable

I'm a bit of a noob with Perl, and can't to see what's wrong with this script:

#!/bin/sh
randAnPass=true;
if [ $randAnPass = true ]
then
pass=perl /root/bin/randpass
else
# prompt for setting user's password ..
echo -n "pick password for '${user}': "
read pass
fi
#echo $randAnPass;
echo "Generated pass = $pass";

For some reason it outputs:

r4Nd0mP
Generated pass = 

I want it to output

Generated pass = r4Nd0mP

If you want to just capture STDOUT of perl command use,

pass=$(perl /root/bin/randpass)

But if you need to capture both STDERR and STDOUT ,

pass=$(perl /root/bin/randpass 2>&1)
pass=`perl /root/bin/randpass`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM