2 identical functions, but one freezes

I can’t shut my brain around why one of these functions works normally [status-up], but the other just hangs without output at all. [Status-REC]

Both pidfile and recfile are valid paths to their respective PID files in /var/run/ , and both contain only the PID number without line breaks or other non-printing characters.

 status-up() { if [ -f ${pidfile} ]; then if ps p $(cat ${pidfile}) >> /dev/null; then printf "Upload running as PID %s\n" $(cat ${pidfile}) return fi fi echo "Upload is not running" } status-rec() { if [ -f ${recfile} ]; then if ps p $(cat ${recfile}) >> /dev/null; then printf "Receive running as PID %s\n" $(cat ${recfile}) return fi fi echo "Receive is not running" } 
+4
source share
1 answer

cat freezes if no input is entered, as well as many other programs. One of your paths is most likely empty, as you can execute the if :

 $ if [ -f ]; then echo "foo"; fi foo 

Once you enter the block, you hang up the cat <empty> . As pointed out by @ruakh, you should make it work with double quoting a variable.

+4
source

Source: https://habr.com/ru/post/1489985/


All Articles