I have created a bash script test.sh
to check if some ports are listening or not. Since I dont want to hard-code either the ports or the command in the shell script, I put them both in a file config_file
like key value pairs. Let me show the file and schell script below;
test.sh
#!/bin/bash
cat config_file| while read port command; do
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
echo "" > /dev/null
else
eval "$command"
fi
done
config_file
80 /bin/bash /home/user/sample_script1.sh
22 /bin/bash /home/user/sample_script2.sh
Both files sample_script1.sh
and sample_script2.sh
are meant to touch
some sample file. When I run ./test.sh
, the sample files gets created correctly (means sample_script1.sh
and sample_script2.sh
got invoked). But I get
./test.sh: line 8: This: command not found
in the terminal. What could be the reason and how to fix this?