Python 如果输出有字符,我需要运行bash命令;如果输出为空,则需要运行其他命令

Python 如果输出有字符,我需要运行bash命令;如果输出为空,则需要运行其他命令,python,linux,bash,scripting,Python,Linux,Bash,Scripting,我有一个mount脚本,当Python命令输出中包含字符时,我需要运行一个命令,如果输出为空,则运行其他命令 例如: ## define a function that launched the zenity username dialog get_username(){ zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Username:" } # define a function that launched t

我有一个mount脚本,当Python命令输出中包含字符时,我需要运行一个命令,如果输出为空,则运行其他命令

例如:

## define a function that launched the zenity username dialog
get_username(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Username:"
}
# define a function that launched the zenity password dialog
get_password(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Password:" --hide-text
}

# attempt to get the username and exit if cancel was pressed.
wUsername=$(get_username) || exit

# if the username is empty or matches only whitespace.
while [ "$(expr match "$wUsername" '.')" -lt "1" ]; do
    zenity --error --title="Error in username!" --text="Please check your username! Username field can not be empty!"  || exit
    wUsername=$(get_username) || exit
done

wPassword=$(get_password) || exit

while [ "$(expr match "$wPassword" '.')" -lt "1" ]; do
    zenity --error --title="Error in password!" --text="Please check your password! Password field can not be empty!" || exit
    wPassword=$(get_password) || exit
done

Save_pwd=$(python -c "import keyring; keyring.set_password('My namespace', 'wUsername', '$wPassword')")

Get_wPassword=$(python -c "import keyring; keyring.get_password('My namespace', '$wUsername')")

echo $Get_wPassword
# mount windows share to mountpoint
#sudo mount -t cifs //$SERVER/$SHARE ${HOME}/${DIRNAME} -o username=${wUsername},password=${Get_wPassword},domain=${DOMAIN}

# show if mounting was OK or failed
#if [ $? -eq 0 ]; then
#       zenity --info --title="Mounting public share succeeded!" --text="Location Documents/Shares/public!"
#else
#       zenity --error --title="Mounting public did not succed!" --text="Please contact system administrator!"
#fi
现在在这个脚本中,我需要首先运行zenity用户名输入。一旦它运行,Python将运行$Get\u wPassword,一旦它给出一个非空的输出,它将使用从$Get\u wPassword获得的用户名和密码运行
mount
命令。如果$Get\u wPassword为空,则我需要使用$Save\u pwd
mount
命令运行密码输入,以便它将密码保存到钥匙圈中,下次脚本运行时,它会从那里获取密码


我怎样才能做到这一点?使用while循环?如果是,你能举一些例子吗?我不熟悉脚本编写。

据我所知,如果一个名为
Get\wPassword
的shell变量为非空,则需要执行某个命令;如果该变量为空,则需要执行另一个命令。幸运的是,空字符串有一个简单的shell测试:

if [ -n "$Get_wPassword" ]
then
    CommandIfNotEmpty
else
    CommandIfEmpty
fi
如果
somestring
的长度非零,则构造
[-n somestring]
返回true,如果字符串为空,则返回false。有关更多详细信息,请参见
manbash

猜猜你到底想做什么,考虑一下:

if [ -n "$Get_wPassword" ]
then
    if sudo mount -t cifs //$SERVER/$SHARE ${HOME}/${DIRNAME} -o username=${wUsername},password=${Get_wPassword},domain=${DOMAIN}
    then
        zenity --info --title="Mounting public share succeeded!" --text="Location Documents/Shares/public!"
    else
        zenity --error --title="Mounting public did not succed!" --text="Please contact system administrator!"
    fi
else
    echo "Password was empty..."
fi

那是一堵可怕的代码墙;你确定不能将其简化为一个MCVE()或SSCCE()——同一事物的不同名称吗?这里有很多代码显然是不必要的(这不同于“显然不是必要的”),我也不清楚你在问什么。你只需要去掉多余的。例如,前两个循环可能被替换为:
wUsername=someone#通常通过zenity获得
wPassword=someone#通常通过zenity获得
。在你的“现在在这个脚本中…”段落中添加一些标点符号可能也会有所帮助。我尝试过改进它。你说“我需要先运行
zenity
username输入”;这似乎是第一个
while
循环。然后,您还可以通过
zenity
循环获取密码。此时,您有一个用户名和密码;现在还不清楚你下一步想做什么。代码运行
Save\u pwd=$(python…
操作;但是接下来您描述了如何执行
Get_wPassword=$(python…
操作,这让人很困惑。我想先运行zenity user input,这样用户就可以输入他/她的用户名,然后如果$Get_wPassword通过python命令从keyring获得该用户名的密码,它将使用python命令给出的密码运行mount命令。如果python命令$Get_wPassword为空,它会向zenity用户请求密码,并使用用户在zenity input中键入的密码运行$Save_pwd and mount命令。请更新问题,而不是添加注释。如果
Get\wPassword
显示为空,我仍然不清楚您是否要返回开始,或者是什么。因此,我可以在mount命令中使用$Get\wPassword,以便它使用$Get\wPassword输出作为我理解的密码?是的。运行语句
Get\u wPassword=$(python…
)后,shell变量
Get\u wPassword
将被分配给该python命令的任何输出。然后,您可以在需要该值的任何位置使用
$Get\wPassword