Common functions of shell scripts

1. The shell gets the configuration from the configuration file

1. Create a new configuration file conf.txt with the following contents:

        path='/data/source'
username='myuser'
password='mypassword'
    

2. Create a new shell file read.sh The content of the file is as follows

         eval `cat ./conf.txt`
echo "$username"
echo "$path"
echo "$password"

      This will get the contents of the configuration file

Second, split the string and traverse the loop

a="1.1.1" #To
split $a, first store the old separator
OLD_IFS="$IFS" #Set the


separator
IFS="."  #The

following will automatically separate
arr=($a) Note: Parentheses Can't lose
echo "arr:" ${arr}
echo "arr length:" ${#arr[@]} #Restore
the original separator
IFS="$OLD_IFS"


3. Dictionary

Uppercase A will report an error. If it is changed to lowercase a, the key can only use numbers

declare -A map=(["sunjun"]="a" ["jason"]="b" ["lee"]="c")


echo ${#map[*]}


#2, take out all the values
echo ${map[@]}
echo ${map[*]}


#3. Take out all keys


echo ${!map[@]}
echo ${!map[*]}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444609&siteId=291194637