1

I need to read the contents of multiple files, for now I can read the contents of just one file using the code below , this code further reads the contents present in "rules1.txt" using "strings" . I just need the same functionality but with multiple files to read in "apps.txt" .

for i in $(cat apps.txt);
do    
cd /local/apps/oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app/$i
echo $i >> /data/shellscripts/essbase/Operate/Overlays/rules1.txt     done
for i in $(ls *.rul);do
echo $i  >> /data/shellscripts/essbase/Operate/Overlays/rules1.txt      
strings $i  >> /data/shellscripts/essbase/Operate/Overlays/rules.txt
  • for i in $(ls *.rul); you should never use ls result. use for i in *.rul this will work better and on all linux you will use your script on. https://unix.stackexchange.com/q/128985/53092 – Kiwy Jun 11 '18 at 07:18

2 Answers2

1

Don't read the whole apps.txt file into a for loop. In the general case, this could cause memory issues if the file is huge, and it would cause issues if any filename in apps.txt had spaces in them. Don't loop over the output of ls. Also, quote variable expansions and use printf for outputting variable data.

Related:

Suggestion:

#!/bin/sh

appdir='/local/apps/oracle/Middleware/user_projects/epmsystem1/EssbaseServer/essbaseserver1/app'
outdir='/data/shellscripts/essbase/Operate/Overlays'

while IFS= read -r app; do
    printf '%s\n' "$app"
    for name in "$appdir/$app"/*.rul; do
        printf '%s\n' "${name##*/}"
        strings "$name"
    done
done <apps.txt >"$outdir/rules1.txt"

This would read app from the apps.txt file and would access all the *.rul files in the corresponding subdirectory under $appdir. The strings $app and ${name##*/} (the basename of the current *.rul file) would be outputted in each iteration, along with the output of strings. The output would go to the $outdir/rules1.txt file.

Also related (to my solution):

Kusalananda
  • 333,661
0

From this post: https://stackoverflow.com/a/1689063/1195001

while read i  ; do
    cd /local/apps/oracle/Middleware/user_projects/epmsystem1/EssbaseServe/essbaseserver1/app/$i
    echo $i >> /data/shellscripts/essbase/Operate/Overlays/rules1.txt
done < apps.txt    

While is better suited than for for this task.

Kiwy
  • 9,534