This is my first attempt to write a bash script and I'm unable to read array outside for loop.
What I'm trying to do is.. store name of all files from /MyDir in an array.
Check if there is process running with that name.
Store name of process running and not running into different arrays.
Print both the arrays and elements count in each array, outside for loop.
Below is the code I'm working on.
Please guide.
#!/bin/bash
declare -a dead
declare -a live
cd /usr/local/MyDir/
FILES=*
for f in $FILES
do
ps -ef | grep $f > /dev/null
if [ $? -eq 0 ];
then
live+=( "$f" )
echo "Process $f is running."
else
dead+=("$f")
echo "Process $f is not running."
fi
done
echo "${[#@live]} Processes are running."
echo "List of Processes live ${live[@]}"
echo "${[#@dead]} Processes are dead."
echo "List of Processes dead ${dead[@]}"