如何在 Unix (Bash) shell 脚本中使用带点的变量?我有下面的脚本:
\n\n#!/bin/bash\nset -x\n"FILE=system.properties"\nFILE=$1\necho $1\nif [ -f "$FILE" ];\nthen\n echo "File $FILE exists"\nelse\n echo "File $FILE does not exist"\nfi\nRun Code Online (Sandbox Code Playgroud)\n\n这基本上就是我需要的 \xe2\x9f\xb6 x=propertyfile,propertyfile=$1.有人可以帮助我吗?
您不能用点声明变量名,但可以使用关联数组来映射键,这是更合适的解决方案。这需要 Bash 4.0。
declare -A FILE ## Declare variable as an associative array.
FILE[system.properties]="somefile" ## Assign a value.
echo "${FILE[system.properties]}" ## Access the value.
Run Code Online (Sandbox Code Playgroud)