In Linux, read parameters from a file and splicing them into the command line

Example:

1. Format the executable command as required:

str2str is an executable file, followed by two parameters -in and -out

./str2str -in ntrip://zy:[email protected]:8080/ABCDEFG -out udpcli://123.249.112.13:8090

The parameters after -in and -out are in a text file, which needs to be read from the text file, and then automatically spliced ​​into a command:

2. The content stored in the text file (config.txt) is as follows:

You can see that in the file, each parameter is separated by a comma (,), so we can read the text file line by line, and use the comma as the separator to get each parameter of each line.

/ABCDEFH,123.249.112.10,8080,zy,123456,123.249.112.13,8090
/ABCDEFG,123.249.112.11,8080,zy,123456,123.249.112.13,8091
...
...
/ABCDEFI,123.249.112.12,8080,zy,123456,123.249.112.13,8092

The corresponding script file is as follows:

#!/bin/bash

# 程序路径
EXEC_PATH=./str2str

# 配置文件路径
CONFIG_PATH=./config.txt

# 命令列表
commands=()
while read line
do
  # 分割字段
  fields=(${line//,/ })

  # 获取ntrip服务器的地址、端口、用户名和密码
  ntrip_ip=${fields[1]}
  ntrip_port=${fields[2]}
  ntrip_user=${fields[3]}
  ntrip_password=${fields[4]}

  # 构造输入参数,格式为 ntrip://[username]:[password]@[ip]:[port]/[mountpoint]
  in_param="ntrip://$ntrip_user:$ntrip_password@$ntrip_ip:$ntrip_port${fields[0]}"

  # 获取输出UDP服务器的地址和端口
  udp_ip=${fields[5]}
  udp_port=${fields[6]}

  # 构造输出参数,格式为 udpcli://[ip]:[port]
  out_param="udpcli://$udp_ip:$udp_port"

  # 构造命令
  command="$EXEC_PATH -in $in_param -out $out_param"

  # 添加到命令列表 使用 nohup 命令将每个命令放在子 shell 中运行,使其在后台运行,并且将标准输出        
  #和标准错误输出重定向到 /dev/null,忽略输出信息
  commands+=("nohup sh -c \"$command\" >/dev/null 2>&1 &")

done < $CONFIG_PATH

# 依次执行命令列表
for cmd in "${commands[@]}"
do
  eval $cmd
done

Code explanation:

(1) Discard output

nohup bash -c "$cmd" >/dev/null 2>&1 &

>/dev/null is a common output redirection operation, which outputs the standard output (stdout) to the /dev/null device, so as to achieve the purpose of discarding the output.

2>&1 is a shell's I/O redirection syntax, which means that the standard error output (stderr) is also redirected to the standard output (stdout), so that all output will be output to >/dev/null, that is, the output is discarded .

Specifically, 2 indicates the file descriptor for standard error output , & indicates the same as standard output, and 1 indicates the file descriptor for standard output . So, 2>&1 means to redirect standard error output to standard output.

(2) Execute the command list in sequence

for cmd in "${commands[@]}":

Traverse the array commands, and assign an element in the array to the variable cmd each time through the loop.

eval $cmd: Use the eval command to execute the command stored in the variable $cmd.

The eval command parses variables and special characters in a command, then executes the command. Use the eval command to dynamically pass the value of the variable to the command when running the command, so as to realize the function of converting the command string into an executable command.

Command to clear a file:

echo "" > config.txt

 

Guess you like

Origin blog.csdn.net/weixin_49561506/article/details/130123863