[英]Shell script function with global variable
昨天我有一個非常簡單的任務,但不幸的是,看起來我無法使用出色的代碼。
任務簡介:我有很多參數,我想用安裝程序腳本中的“交互式”模式詢問。
詳細代碼:
#!/bin/bash
address="192.168.0.1" # default address, what the user can modify
addressT="" # temporary variable, that I want to check and modify, thats why I don't modify in the function the original variable
port="1234"
portT=""
... #there is a lot of other variable that I need for the installer
function parameter_set {
$1=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3" 3>&1 1>&2 2>&3) # thats the line 38
}
parameter_set "addressT" "Please enter IP address!" "$address"
parameter_set "portT" "Please enter PORT!" "$port"
但是我收到以下錯誤:
"./install.sh: line: 38: address=127.0.0.1: command not found"
如果我將變量修改為另一個變量(不是函數的參數),則效果很好。
function parameter_set {
foobar=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3" 3>&1 1>&2 2>&3)
echo $foobar
}
我嘗試使用全局retval變量,並在函數外部將其分配給原始變量,它可以工作,但是我認為這不是解決此任務的最佳解決方案。
誰能幫助我,我做錯了什么? :)
在此先感謝您(感謝我的英語不好。),阿提拉
由於重定向,您的whiptail命令似乎未產生任何輸出。 因此,命令替換將值保留為空。 嘗試刪除它們。 另外最好先將新值保存到局部變量中:
parameter_set() {
local NAME=$1
local NEWVALUE=$(whiptail --title "Installer" --inputbox "$2" 12 60 "$3")
export $NAME="$NEWVALUE"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.