Welcome to the Linux Foundation Forum!
Bash scripting assistance needed please.
Digiza
Posts: 11
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
/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.
0
Comments
-
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 done0 -
I feel like a Grade A dunce:blink: , Thanks alot, worked perfectly mfillpot.B)0
-
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" done0 -
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.
Regards0 -
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.0
Categories
- All Categories
- 178 LFX Mentorship
- 178 LFX Mentorship: Linux Kernel
- 772 Linux Foundation IT Professional Programs
- 382 Cloud Engineer IT Professional Program
- 175 Advanced Cloud Engineer IT Professional Program
- 75 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 102 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 7 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 19 Cloud & Containers Training
- 2 Cybersecurity Training
- 2 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 2 Networking Training
- 2 Open Source Best Practice Training
- 5 System Administration Training
- 1 System Engineering Training
- 3 Web & Application Development Training
- 797 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 91 Storage
- 771 Linux Distributions
- 81 Debian
- 68 Fedora
- 23 Linux Mint
- 13 Mageia
- 24 openSUSE
- 151 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 467 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 99 Linux Security
- 79 Network Management
- 101 System Management
- 46 Web Management
- 135 Mobile Computing
- 20 Android
- 100 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 402 Off Topic
- 125 Introductions
- 33 Study Material
- 1K Programming and Development
- 310 Kernel Development
- 707 Software Development
- 1K Software
- 415 Applications
- 182 Command Line
- 5 Compiling/Installing
- 71 Games
- 319 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)