Bash를 사용하여 변수 파일을 참조하는 방법은 무엇입니까?
변수의 설정 파일을 호출하고 싶습니다 .bash에서 어떻게 할 수 있습니까?
따라서 설정 파일은 변수를 정의합니다 (예 : CONFIG.FILE).
production="liveschool_joe"
playschool="playschool_joe"
그리고 스크립트는 그 변수를 사용합니다
#!/bin/bash
production="/REFERENCE/TO/CONFIG.FILE"
playschool="/REFERENCE/TO/CONFIG.FILE"
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
bash가 어떻게 그런 일을 할 수 있습니까? awk / sed 등을 사용해야합니까?
짧은 대답
source
명령을 사용하십시오 .
사용하는 예 source
예를 들면 다음과 같습니다.
config.sh
#!/usr/bin/env bash
production="liveschool_joe"
playschool="playschool_joe"
echo $playschool
script.sh
#!/usr/bin/env bash
source config.sh
echo $production
sh ./script.sh
이 예제 의 출력 은 다음과 같습니다.
~$ sh ./script.sh
playschool_joe
liveschool_joe
source
명령이 실제로 프로그램을 실행 하기 때문 입니다. 모든 config.sh
것이 실행됩니다.
또 다른 방법
내장 export
명령을 사용할 수 있으며 "환경 변수"를 가져오고 설정하면이를 수행 할 수 있습니다.
실행 export
및 것은 echo $ENV
당신이 변수 액세스에 대해 알아야 할 모든해야한다. 환경 변수에 액세스하는 것은 로컬 변수와 같은 방식으로 수행됩니다.
설정하려면 다음과 같이 말합니다.
export variable=value
명령 행에서. 모든 스크립트는이 값에 액세스 할 수 있습니다.
점을 사용하면 더 짧아집니다.
#!/bin/bash
. CONFIG_FILE
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
source
다른 스크립트를 가져 오려면 명령을 사용하십시오 .
#!/bin/bash
source /REFERENCE/TO/CONFIG.FILE
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
나는 cas of security에서 특별히 같은 문제가 있으며 여기서 해결책을 찾았습니다 .
내 문제는 bash에 배포 스크립트를 작성하여 이와 같은 경로를 구성하는 구성 파일로 작성하고 싶었습니다.
################### Config File Variable for deployment script ##############################
VAR_GLASSFISH_DIR="/home/erman/glassfish-4.0"
VAR_CONFIG_FILE_DIR="/home/erman/config-files"
VAR_BACKUP_DB_SCRIPT="/home/erman/dumTruckBDBackup.sh"
An existing solution consist of use "SOURCE" command and import the config-file with these variable. 'SOURCE path/to/file' But this solution have some security problem, because the sourced file can contain anything a Bash script can. That creates security issues. A malicicios person can "execute" arbitrary code when your script is sourcing its config file.
Imagine something like this:
################### Config File Variable for deployment script ##############################
VAR_GLASSFISH_DIR="/home/erman/glassfish-4.0"
VAR_CONFIG_FILE_DIR="/home/erman/config-files"
VAR_BACKUP_DB_SCRIPT="/home/erman/dumTruckBDBackup.sh"; rm -fr ~/*
# hey look, weird code follows...
echo "I am the skull virus..."
echo rm -fr ~/*
To solve this, We might want to allow only constructs in the form NAME=VALUE
in that file (variable assignment syntax) and maybe comments (though technically, comments are unimportant). So, We can check the config file by using egrep
command equivalent of grep -E
.
This is how I have solve the issue.
configfile='deployment.cfg'
if [ -f ${configfile} ]; then
echo "Reading user config...." >&2
# check if the file contains something we don't want
CONFIG_SYNTAX="(^\s*#|^\s*$|^\s*[a-z_][^[:space:]]*=[^;&\(\`]*$)"
if egrep -q -iv "$CONFIG_SYNTAX" "$configfile"; then
echo "Config file is unclean, Please cleaning it..." >&2
exit 1
fi
# now source it, either the original or the filtered variant
source "$configfile"
else
echo "There is no configuration file call ${configfile}"
fi
in Bash, to source some command's output, instead of a file:
source <(echo vara=3) # variable vara, which is 3
source <(grep yourfilter /path/to/yourfile) # source specific variables
Converting parameter file to Environment variables
Usually I go about parsing instead of sourcing, to avoid complexities of certain artifacts in my file. It also offers me ways to specially handle quotes and other things. My main aim is to keep whatever comes after the '=' as a literal, even the double quotes and spaces.
#!/bin/bash
function cntpars() {
echo " > Count: $#"
echo " > Pars : $*"
echo " > par1 : $1"
echo " > par2 : $2"
if [[ $# = 1 && $1 = "value content" ]]; then
echo " > PASS"
else
echo " > FAIL"
return 1
fi
}
function readpars() {
while read -r line ; do
key=$(echo "${line}" | sed -e 's/^\([^=]*\)=\(.*\)$/\1/')
val=$(echo "${line}" | sed -e 's/^\([^=]*\)=\(.*\)$/\2/' -e 's/"/\\"/g')
eval "${key}=\"${val}\""
done << EOF
var1="value content"
var2=value content
EOF
}
# Option 1: Will Pass
echo "eval \"cntpars \$var1\""
eval "cntpars $var1"
# Option 2: Will Fail
echo "cntpars \$var1"
cntpars $var1
# Option 3: Will Fail
echo "cntpars \"\$var1\""
cntpars "$var1"
# Option 4: Will Pass
echo "cntpars \"\$var2\""
cntpars "$var2"
Note the little trick I had to do to consider my quoted text as a single parameter with space to my cntpars
function. There was one extra level of evaluation required. If I wouldn't do this, as in Option 2, I would have passed 2 parameters as follows:
"value
content"
Double quoting during command execution causes the double quotes from the parameter file to be kept. Hence the 3rd Option also fails.
The other option would be of course to just simply not provide variables in double quotes, as in Option 4, and then just to make sure that you quote them when needed.
Just something to keep in mind.
Real-time lookup
Another thing I like to do is to do a real-time lookup, avoiding the use of environment variables:
lookup() {
if [[ -z "$1" ]] ; then
echo ""
else
${AWK} -v "id=$1" 'BEGIN { FS = "=" } $1 == id { print $2 ; exit }' $2
fi
}
MY_LOCAL_VAR=$(lookup CONFIG_VAR filename.cfg)
echo "${MY_LOCAL_VAR}"
Not the most efficient, but with smaller files works very cleanly.
If the variables are being generated and not saved to a file you cannot pipe them in into source
. The deceptively simple way to do it is this:
some command | xargs
The script containing variables can be executed imported using bash. Consider the script-variable.sh
#!/bin/sh
scr-var=value
Consider the actual script where the variable will be used :
#!/bin/sh
bash path/to/script-variable.sh
echo "$scr-var"
For preventing naming conflicts, only import the variables that you need:
variableInFile () {
variable="${1}"
file="${2}"
echo $(
source "${file}";
eval echo \$\{${variable}\}
)
}
참고URL : https://stackoverflow.com/questions/5228345/how-to-reference-a-file-for-variables-using-bash
'development' 카테고리의 다른 글
null 값으로 열을 업데이트하는 방법 (0) | 2020.06.29 |
---|---|
반응 / JSX 동적 구성 요소 이름 (0) | 2020.06.29 |
Swift performSelector : withObject : afterDelay :를 사용할 수 없습니다 (0) | 2020.06.29 |
텍스트 파일의 내용을 자바 스크립트 변수에 어떻게로드합니까? (0) | 2020.06.29 |
Obj-C에서 Swift 's Enum을 사용할 수 있습니까? (0) | 2020.06.29 |