I'm no pro when it comes to BASH, but I have been known to shell-script my way out of a problem here and there. One of the useful things you can do is a for-loop, whether it be used on file names, script arguments, or just a string of words. Here are some very basic examples on how to do for-loops in BASH.
Basic for-loops with strings, arrays, file names, and arguments.
#!/bin/bash
printf "Here are some basic loops with arrays, strings, file names, and arguments.\n"
# Array of strings
printf "\nArray of strings:\n"
myvalues=("value1" "value2" "value3")
for value in ${myvalues[@]}
do
echo " value: "$value
done
# String with spaces in it.
printf "\nString with spaces:\n"
myvalues2="my short string"
for value in $myvalues2
do
echo " test: "$value
done
# All files in directory
printf "\nAll Files:\n"
for filename in *
do
echo " found file: "$filename
done
# All shell scripts in current directory
printf "\nAll shell scripts:\n"
for shellscript in *.sh
do
echo " found shell script: "$shellscript
done
# arguments
if [ -z "$1" ]; then
printf "\nYou didn't pass any arguments, pass some for this test.\n"
else
printf "\nArguments passed:\n"
for argument in $@
do
echo " argument: "$argument
done
fi
printf "\nFinished.\n"
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.