This might be a naive question, but I am new to bash scripting. I am trying to open a file and count the number of lines in that file and if it is greater than 4, then output something. I have figured out how to pass and save the line count to a variable, but I cannot access the file in the bash when I do not pass it the exact name. Therefore, I have this program called new_test written as so:
test_function()
{
file_name='test_text.txt'
n=$(wc -l < $file_name)
if [ 'n > 2' ]
then
echo "Too many lines"
fi
}
test_function
This program works, but when I try and make it more generic to accept any file by changing line 3 from:
file_name='test_text.txt'
to
file_name=$1
And then call
./new_test test_text.txt
it outputs:
./test_file[4]: : cannot open
From my understanding, the $1 is the second argument passed, and in the case of ./new_test test_text.txt, this would be the test_text.txt file. Do I need to obtain the address of the second argument or is the notation different in order to do this? Thank you in advance.