通过exe传递变量

通过exe传递变量
#!/bin/bash
# useradd1.sh - A simple shell script to display the form dialog on screen
# set field names i.e. shell variables
Name=""
OPAC=""
Intranet=""
mysqlroot=""
password=""
# open fd
# exec 3>&1
exec 3<&0

# dialog --form text height width formheight [ label y x item y x flen ilen ]
# note - 0 - stdin, 1 - stdout, 2 - stderr
# Store data to $VALUES variable
VALUES=$(dialog --ok-label "Submit" \
      --backtitle "A script for automated Koha instance creation, developed by ..." \
      --title "Automated Koha instance creation - Dashboard" \
      --form "Enter the required information.... " \
15 65 5 \
    "Enter Koha instance name:"         1 1     "$Name"         1 40 12 0 \
    "Enter the port for Koha OPAC:"     2 1     "$OPAC"         2 40 8 0 \
    "Enter the port for Koha Intranet:"     3 1     "$Intranet"         3 40 8 0 \
    "Enter the root password for MySQL:"    4 1     "$mysqlroot"        4 40 12 0 \
    "Set   the loging password for Koha:"   5 1     "$password"     5 40 12 0 \
2>&1 1>&3)

# close fd
# exec 3>&-
exec >&3
# exec < $OPAC

# display values just entered
#echo "You have entered"\ 
#echo "$VALUES"
echo Entering next step...

#Port Declaration:

#echo "Declare Your First PORT Number (except 8005):"
#read OPAC
#echo "Declare Your Second PORT Number:"
#read Intranet
cd
sudo sed -i -e "5 a\Listen $OPAC" -e "5 a\Listen $Intranet" /etc/apache2/ports.conf
exec 3>&-
echo Done....

我无法传递变量$OPAC$Intranet目标文件。它只是打印 Listen

答案1

dialog未就地更新 $OPAC:您必须从 $VALUES 数据中提取用户信息。之前添加这段代码echo Entering next step...

{
read -r Name
read -r OPAC
read -r Intranet
read -r mysqlroot
read -r password
} <<<"$VALUES"

echo === debug info
echo "Name=>$Name<"
echo "OPAC=>$OPAC<"
echo "Intranet=>$Intranet<"
echo "mysqlroot=>$mysqlroot<"
echo "password=>$password<"
echo ===

如果您对此感到满意,请注释掉调试echo命令。


解决此问题的另一种方法是使用readarray命令捕获对话框的输出。这不会将数据放入单个字符串中,而是放入一个从 0 开始的索引数组中,每行一个值:

readarray -t data < <( dialog ... 2>&1 1>&3 )
echo "debug: user data"
declare -p data
# ...
sudo sed -i -e "5 a\Listen ${data[1]}" -e "5 a\Listen ${data[2]}" /etc/apache2/ports.conf

相关内容