How can i read the contents of multiple files?

5,665

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):

Share:
5,665

Related videos on Youtube

user3851659
Author by

user3851659

Updated on September 18, 2022

Comments

  • user3851659
    user3851659 almost 2 years

    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
    
    • S edwards
      S edwards about 6 years
      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. unix.stackexchange.com/q/128985/53092
  • user3851659
    user3851659 about 6 years
    its unable to recognize the files within apps.txt with while
  • S edwards
    S edwards about 6 years
    What you mean ? What is not working how it's not working ?