简体   繁体   English

读取txt文件并将值解析为bash脚本

[英]Read txt file and parse the values to bash script

I have the following bash script: 我有以下bash脚本:

#!/bin/bash

filename='config.txt'

while filename = read -r line do
        for file in $(find /home/user/ftpuser -maxdepth 1 -name "*.[ew]ar" -type f); do
        /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs com.lib - input ../ftpuser/ -output ../reports/ "${file}"
            cp "${file}" /home/user/ftpuser/scanned/
        done < "$filename"
        sleep 60
done

The script needs to find two types of files into a directory. 该脚本需要在目录中找到两种类型的文件。 An .ear and a .war file. .ear和.war文件。 Once it finishes it executes one command which is making reports of the files that we have found before in a directory called /reports. 完成后,它会执行一个命令,该命令将报告以前在/ reports目录中找到的文件。 The next step is to copy all the files that we have found in the step number one, to a directory called scanned. 下一步是将在第一步中找到的所有文件复制到一个名为scand的目录中。 My problem focuses in the command I am executing to make the reports. 我的问题集中在我正在执行以生成报告的命令中。 In this command there is somewhere the -javaPkgs com.lib. 在此命令中,有-javaPkgs com.lib。 I need to read this value from a configuration file.The script needs to read the values from the configuration file and assign them to the script, so each time we change the values in the configuration file we can execute the script with different values. 我需要从配置文件中读取此值。脚本需要从配置文件中读取值并将它们分配给脚本,因此,每次更改配置文件中的值时,都可以使用不同的值执行脚本。 My question is how can I do this? 我的问题是我该怎么做? I have tried above to do something but it doesn't work. 我已经在上面尝试做一些事情,但是没有用。

Below you can also see the configuration file. 在下面您还可以看到配置文件。

config.txt config.txt

targetHostName=10.125.162.132
packages=com.ibm,com.jboss   
path=/home/user/ftpuser/reports
username=root
password=root

The following would give you a list of packages for which you want the reports: 以下将为您提供您想要报告的软件包列表

grep "^packages" config.txt | cut -d= -f2 | tr ',' ' '

Based on this, you can loop for values in the list: 基于此,您可以循环搜索列表中的值:

filename="config.txt"
for i in $(grep "^packages" $filename | cut -d= -f2 | tr ',' ' '); do
  for file in $(find /home/user/ftpuser -maxdepth 1 -name "*.[ew]ar" -type f); do
    echo /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs ${i} - input ../ftpuser/ -output ../reports/ "${file}"
    cp "${file}" /home/user/ftpuser/scanned/
  done
done

This is how I see how you could use it: 这是我如何使用它的方式:

#!/bin/bash

config_file='./config.txt'  ## If you want to pass your configuration as an argument, use config_file=$1

. "$config_file"

while read -r file do
    /apps/oracle/jrockit/4.1.0-1.6.0_37-R28.2.5-x86_64/bin/java -jar ../windup-cli-0.6.8/windup-cli.jar -javaPkgs com.lib - input ../ftpuser/ -output ../reports/ "${file}"
    cp "${file}" /home/user/ftpuser/scanned/
    sleep 60
done < <(exec find /home/user/ftpuser -maxdepth 1 -name '*.[ew]ar' -type f)

The script would read config_file.txt as another source file assigning values to variables. 该脚本将读取config_file.txt作为另一个为变量分配值的源文件。 With that you could already use those variables as $targetHostName , $packages , $path , $username and $password . 这样,您就可以将这些变量用作$targetHostName$packages$path$username$password

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM