bash脚本包含perl代码 - 并在Python上运行

时间:2015-09-16 07:21:42

标签: python bash perl shell unix

我试图在Python上运行bash脚本 - 其中包含perl代码。 出于某种原因 - 它只执行bash部分而不是perl部分。

调用bash脚本的Python代码是:(2个选项)

 bashCommand = "/bin/bash createUser.sh "+  str(self.emptyNameField.text()) +" "+ str(self.emptyPWField.text())+" " + str(self.emptyGroupField.text())
        os.system(bashCommand)

subprocess.call([path+'createUser.sh', str(self.emptyNameField.text()),str(self.emptyPWField.text()),str(self.emptyGroupField.text())])

bash脚本:

#!/bin/bash

USER_NAME="$1"
PASSWORD="$2"
UNIQUE_ID="$3"
sudo dscl . create /Users/$USER_NAME
sudo dscl . create /Users/$USER_NAME UserShell /bin/bash
sudo dscl . create /Users/$USER_NAME RealName "$USER_NAME"
sudo dscl . create /Users/$USER_NAME UniqueID $UNIQUE_ID
sudo dscl . create /Users/$USER_NAME PrimaryGroupID 1000
sudo dscl . create /Users/$USER_NAME NFSHomeDirectory /Local/Users/$USER_NAME
sudo dscl . passwd /Users/$USER_NAME $PASSWORD
export USER_NAME
export PASSWORD
export UNIQUE_ID


script=$(cat <<'EOF'

$file = "UsersList.txt";

open LIST, '>>',$file;
print LIST "$ENV{USER_NAME} @ $ENV{PASSWORD}\n";
close(LIST);
EOF
)
perl -e "$script"


echo "User Name: $USER_NAME , Password: $PASSWORD "

任何帮助都将非常感谢!!!

1 个答案:

答案 0 :(得分:2)

好吧,我不能说将你的shell脚本折叠成python,但perl所做的就是:

echo ${USERNAME} \@ ${PASSWORD} >> UserList.txt

对我来说似乎有点矫枉过正。