'Local keyword variable in bash producing [Not a directory] errors
I have a bash script that has 4 functions. One to output the error message:
error_message()
{
local err_msg=${1}
echo "Error: ${err_msg}"
}
One to check if the variable is empty:
empty_check()
{
local name=${1}
local val=${2}
if [[ -z "{val// }" ]]
then
error_message "$name is empty"
exit 1
fi
}
One to check if the file exists:
exist()
{
local f_name="${1}"
empty_check f_name ${f_name}
if [[ -f ${f_name} ]]
then
return 0
else
return 1
fi
}
And then one that checks the number of lines in the file and if the file exists:
file_check()
{
direc=$1
file_n=$2
count=$(find $dir_name -name $file_name | wc -l)
lines=$(wc -l < "$direc/$file_n")
if [ "$(exist $file_n)" -eq 1 ]
then
echo "Error: File does not exist"
else
echo "The number of lines in the file are $lines"
fi
}
I have a directory called some_directory with a file inside called test_file and I execute the function that I need which is file_check at the bottom of the script by calling:
some_directory="/dir/test_directory"
file_check $some_directory test_file
The 2 functions that use the local keyword (empty_check() and exist()) produce this error:
./file_scr.sh[3]: local: cannot execute [Not a directory]
./file_scr.sh[4]: local: cannot execute [Not a directory]
./file_scr.sh[10]: local: cannot execute [Not a directory]
./file_scr.sh[11]: local: cannot execute [Not a directory]
If I remove the local keyword from them, then it works correctly, but I cannot do this. Can anyone guide me as to why this occurring and how to modify my file_check() so that it works with these local keywords? Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
