我需要制作在特定变量下运行多个命令的 bash 脚本,即,如果我输入 bash appserver
(例如),它会运行以下所有命令
,但我需要将它们放在块中:
appserver
[
read -e -p "please enter weblogic username: " user
read -e -s -p "please enter weblogic password: " pass
echo ""
echo 'JAVA_OPTIONS="-Dweblogic.management.username='$user' -Dweblogic.management.password='$pass'"; export JAVA_OPTIONS' > /home/oracle/ofm_start.sh
echo "# Start NodeManager" >> /home/oracle/ofm_start.sh
]
答案1
只需将命令放入带有舍邦线:
#!/usr/bin/env bash
read -e -p "please enter weblogic username: " user
read -e -s -p "please enter weblogic password: " pass
echo ""
echo "JAVA_OPTIONS=\"-Dweblogic.management.username='$user' -Dweblogic.management.password='$pass'\"; export JAVA_OPTIONS" > /home/oracle/ofm_start.sh
echo "# Start NodeManager" >> /home/oracle/ofm_start.sh
请注意,我更正了您行中的引用,echo
因为您的原始版本没有保留单引号。
然后,使用 使脚本可执行chmod +x foo.sh
并使用 运行它./foo.sh
。
答案2
bash
有功能像许多其他编程语言一样。
function appserver
{
read -e -p "please enter weblogic username: " user
read -e -s -p "please enter weblogic password: " pass
echo ""
echo 'JAVA_OPTIONS="-Dweblogic.management.username='$user' -Dweblogic.management.password='$pass'"; export JAVA_OPTIONS' > /home/oracle/ofm_start.sh
echo "# Start NodeManager" >> /home/oracle/ofm_start.sh
}