変数値を変更するBashスクリプト機能

変数値を変更するBashスクリプト機能

パラメータの1つ(変数など)の値を変更するBASHスクリプト用の関数を作成したいと思います。私が作成した機能は次のとおりです。

chck_rgx () { # Checks that the given name for a user/group fulfills the regex. The function's parameters are                                                                                                         
              #   * $1: kind of name to check: username, hostname, etc.
              #   * $2: variable whose value to check if meets the requirements.                         
  local rgx_hostname='^[a-z][a-zA-Z0-9_-]+$' ;                                                                                          
  local rgx_inputbox_hostname="The hostname you just submitted isn't a valid name. Try with another name."
  local rgx_username='^[a-z][a-z0-9_-]+$' ;
  local rgx_inputbox_username="The username you just submitted isn't a valid name. Try with another name." ;
  eval rgx_name="\$rgx_$1" ;                                                                                                            
  eval rgx_inputbox="\$rgx_inputbox_$1" ;                                                                                               
  eval name_ch="\$$2" ;                                                                                                                 
  while [[ ! $name_ch =~ $rgx_name ]] ; do                                                                                              
    dialog --backtitle "$backtitle_var" \       
           --title     "Wrong $1 submitted" --clear \                                                           
           --inputbox  "$rgx_inputbox" 0 0 2> name-ch ;                                                                              
    name_ch=$(cat name-ch) ;                                                                                                            
    rm name-ch ;                                                                                                                        
  done                                                                                                                                  
  chckd_var="$name_ch" ;
}

その後、スクリプトの一部に名前を入力するダイアログが表示され、関数はその名前が有効な名前であることを確認します。例えば、

dialog  --backtitle "$backtitle_var" \                                                                                              
        --title     "Submit the username" --clear \                                                                  
        --inputbox  "Which username do you want?" 0 0 2> user-name ;                                             
user_name=$(cat user-name) ;                                                                                                          
chck_rgx username user_name ;                                                                                                 
user_name=$chckd_var ;                 

私が経験している問題は、私の関数の「出力」、つまり私が今持っている値が元々使用したのと同じ変数にないことです。関数パラメーターとして挿入した変数(user_name)に、関数($ chckd_var)の「出力」値を割り当てる必要があります。私が望むのは、関数がグローバル変数user_name(または他の変数)の値を変更することです。

また、ダイアログボックスの入力ボックス出力をファイルではなく変数に直接インポートする方法があるかどうか疑問に思います。

私はBASHスクリプトに初めて触れたので、答えは簡単かもしれません。とにかくこの問題についてオンラインで探してみましたが、答えが見つかりませんでした。

ベストアンサー1

さて、最初の質問に対する答えは、Stack Exchangeで質問のタイトルを書いたときにした提案を通して見つけたようです。これを変更すると、次のようにわかりそうです。

chck_rgx () { # Checks that the given name for a user/group fulfills the regex. The function's parameters are                                                                                                         
              #   * $1: kind of name to check: username, hostname, etc.
              #   * $2: variable whose value to check if meets the requirements.                         
  local rgx_hostname='^[a-z][a-zA-Z0-9_-]+$' ;                                                                                          
  local rgx_inputbox_hostname="The hostname you just submitted isn't a valid name. Try with another name."
  local rgx_username='^[a-z][a-z0-9_-]+$' ;
  local rgx_inputbox_username="The username you just submitted isn't a valid name. Try with another name." ;
  eval rgx_name="\$rgx_$1" ;                                                                                                            
  eval rgx_inputbox="\$rgx_inputbox_$1" ;                                                                                               
  eval name_ch="\$$2" ;                                                                                                                 
  while [[ ! $name_ch =~ $rgx_name ]] ; do                                                                                              
    dialog --backtitle "$backtitle_var" \       
           --title     "Wrong $1 submitted" --clear \                                                           
           --inputbox  "$rgx_inputbox" 0 0 2> name-ch ;                                                                              
    name_ch=$(cat name-ch) ;                                                                                                            
    rm name-ch ;                                                                                                                        
  done                                                                                                                                  
  declare -g -- "$2=$name_ch" ;
}

変化は

declare -g -- "$2=$name_ch"

とにかく、ダイアログボックスの入力ボックスの出力をファイルではなく変数として直接取得する方法があるかどうかを知りたいです。

ありがとうございます。

おすすめ記事