Welcome to the Linux Foundation Forum!

Bash scripting assistance needed please.

Hey there Guys / Girls,

I am busy doing 100 different things and configurations on one of my systems and as such I have had places where folders and paths were not existing / missing.

So I wrote this little guy to let me know.

FOLDERS="/u01/stuffs/data
/u01/stuffs/music
/u01/stuffs/information"
for f in $FOLDERS
do
echo "$f"
if [[ -d "${f}" [[ ; then
echo "WOHOO its there"
echo ""
fi
echo "Bummer no path like that."
echo ""
done


Now I am adding it to a little bash applet where it will load various things. one being this script. BUT heres the issue.

How can i get the FOLDERS variable to read from a file.

so for instance have a folders.txt with all the paths listed in it, then my variable Folders will just be a read of that entire file.

everywhere i can find something they are always read 1 line to 1 variable, and thats where im stuck, I would like to keep it 1 variable. somehow.......

Thanks for any assists.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • Posts: 2,177
    Have you tried the following?
    1. for f in $(cat FOLDERS.txt)
    2. do
    3. echo "$f"
    4. if [[ -d "${f}" [[ ; then
    5. echo "WOHOO its there"
    6. echo ""
    7. else
    8. echo "Bummer no path like that."
    9. echo ""
    10. fi
    11. done
  • Posts: 11
    I feel like a Grade A dunce:blink: , Thanks alot, worked perfectly mfillpot.B)
  • Posts: 216
    also, watch out for spaces in the filenames. this got me before. i did something like this:
    1. #!/bin/bash
    2. newline='
    3. '
    4.  
    5. OIFS=$IFS
    6. IFS=$newline
    7. declare -a files
    8. files=($(find . -type f))
    9. IFS=$OIFS
    10.  
    11. for (( i=0 ; $i<${#files[*]}; i++ )); do
    12. file=${files[$i]}
    13. echo "file $i: $file"
    14. done
  • Posts: 647
    Even better:

    folders.txt contents:
    1. /home/marc
    2. /home/marc/test1
    3. /home/marc/test2
    1. unset FOLDERS i
    2. while IFS= read -r -d $'\n' folder; do
    3. FOLDERS[i++]="$folder"
    4. done < folders.txt
    5. echo "${FOLDERS[@]}"

    Suggestions:

    1- Don't use capital names for variables. By convention that is used for environment variables
    2- Do you really need to keep the folders in an array? Why if you already have the list in a file? ;)
    3- This won't deal with "newline" characters in folder names as it won't work with "does format" files either (lines don't end with a "newline" character)

    About what mfillpot said:
    - do *not* use cat for that!!! It's totally unnecessary and resource consuming: it's calling an external program (cat) and sending the output back to the main program. Even if it seems to work, you'd be surprised how many headaches you'll find in the future.

    Regards
  • Posts: 2,177
    marc wrote:
    About what mfillpot said:
    - do *not* use cat for that!!! It's totally unnecessary and resource consuming: it's calling an external program (cat) and sending the output back to the main program. Even if it seems to work, you'd be surprised how many headaches you'll find in the future.

    Regards[/quote]

    That is a great recommendation, this will help on larger files and files with troublesome characters.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training