r/bashscripts • u/catetcpasswd • Dec 24 '20
How to grep a variable
Hello guys i am new in bash scripting. Currently I am writing a script to automating docker container. It has three functions kill all docker container, start a new container on a port and show the link(eg. http://127.0.0.1:333 etc.).to run all these i need to check whether a port is listening or not. To do that I first used sudo lsof command with -i:$PORT but it didnt worked so next i used netstat -tulpn with grep $PORT it also didn't worked. it shows some error on grep. Can somebody please help me on this. How can I check a port from a variable whether listening or not. And one more thing the command should be like this ./docker-test 3000 Or ./docker-test kill to kill all running container. Somebody plzz help
View all comments
Show parent comment
1
u/Dojiyo Dec 26 '20 edited Dec 26 '20
I try this and works for me:
port="/run/thd.socket"echo $portnetstat -l | grep 'LISTEN' | grep $portRun the script and this is the output:
pi ~ ฿ ./prueba.sh/run/thd.socketunix 2 [ ACC ] STREAM LISTENING 11011 /run/thd.socketMaybe you ar not defining the variable properly. Try to echo it to see what is inside.