简体   繁体   中英

Read txt file and parse the values to bash script

I have the following bash script:

#!/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. Once it finishes it executes one command which is making reports of the files that we have found before in a directory called /reports. The next step is to copy all the files that we have found in the step number one, to a directory called scanned. My problem focuses in the command I am executing to make the reports. In this command there is somewhere the -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

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. With that you could already use those variables as $targetHostName , $packages , $path , $username and $password .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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