Welcome to the new 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.

Comments

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

    folders.txt contents:
    /home/marc
    /home/marc/test1
    /home/marc/test2
    
    unset FOLDERS i
    while IFS= read -r -d $'\n' folder; do
        FOLDERS[i++]="$folder"
    done < folders.txt
    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
  • mfillpotmfillpot Posts: 2,180
    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.
Sign In or Register to comment.