A brief analysis of the "Swiss Army Knife" of writing Linux scripts in embedded development

In the embedded Linux development process, when the development board starts the process, you first need to run some shell scripts to configure the system. We know that everything in Linux is a file. If you need to write a program to read the configuration file or log printing information, it will inevitably greatly reduce the development efficiency. This article briefly analyzes the practical application of the very efficient Linux shell command awk in development projects.

1. Obtain useful information from the printing of system commands.

Demo:

The picture above shows the network information of the development board. Now we can get the IP and netmask from it.

Therefore, to obtain the IP and mask address in the shell script, only the following two lines are needed.

ip=$( ifconfig eth0 | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}' )
mask=$( ifconfig eth0 | grep 'Mask' | awk -F: '{print $4}' )

Analysis :

1. The command ifconfig eth0 can obtain the network information corresponding to the network card eth0;

       

2. Let’s talk about the above printing through the pipe ' | ' and hand it over to grep 'inet addr' for processing;

      

3. Use grep to find the line containing "inet addr" and print it out, and then pass it to awk -F: '{print $2}' through the pipe for processing;

The awk -F: here means to separate the input information with ':'. The default parameters of awk are separated by spaces or tabs by default.

'{print $2}' means printing out the second field separated by:, which is 192.168.1.101 Bcast.

$0 means to get the entire row, $2,$3... means to get the 2nd and 3rd field information.

4. Then print the above and run awk '{print $1}' again to get 192.168.101.

 

Among them, grep is a very efficient search command. You can check the usage in the liux terminal grep.

 

Awk is a very powerful text analysis tool. You can enter awk in the linux terminal to view the usage

 

2. Obtain the information at the specified position of a certain line from the text.

Demo:

Here we take the classInfo.ini file as an example.

Get the value of wangwu from the file and copy it to the variable in the shell for system configuration.

Just need to use in shell script

readIni=`awk -F '=' '/\['class2'\]/{a=1}a==1&&$1~/'wangwu'/{print $2;exit}' ./classInfo.ini`

1. Among them, /\['class2'\]/{a=1} means that when awk processes the classInfo.ini file line by line, when it matches '[class2]', set a=1;

2. a==1&&$1~/'wangwu'/ {print $2;exit} means when a==1, and the next line obtained contains /'wangwu'/, that is (wangwu=300) according to the previous - After F '=' separation, print $2, which is the second field information ($1 is wangwu, $2 is 300).

In shell scripts, the above commands are generally encapsulated into a function and called, that is:

function __readINI() {
      INIFILE=$1;    SECTION=$2;    ITEM=$3
      _readIni=`awk -F '=' '/\['$SECTION'\]/{a=1}a==1&&$1~/'$ITEM'/{print $2;exit}' $INIFILE`
      echo ${_readIni}
}

Use directly when calling:

value=$( __readINI ./classInfo.ini class2 wangwu)

 

Guess you like

Origin blog.csdn.net/yufeng1108/article/details/105847713