2021.01.10 00:00

Since the import lines in python can take several seconds (in Raspberry Pi), it’s better to just use a simple bash when trying to do simple stuff.

In bash scripts, I often see /dev/null but never really sure what it’s used for. Also, there’s a lot of different > like 2>/dev/null or &> /dev/null or > /dev/null 2>&1 etc.

/dev/null is basically a way to suppress output. 1 refers to stdout, and 2 refers to stderr. Basically &> /dev/null a new syntax for > /dev/null 2>&1.

Example:

  • ls by itself will show the result in terminal
  • ls 1> /dev/null will not show the result in terminal
  • ls 2> /dev/null will show the result in terminal
  • ls &> /dev/null will not show the result in terminal
  • ls -0 1> /dev/null will show the error in terminal
  • ls -0 2> /dev/null will not show the error in terminal
  • ls -0 &> /dev/null will not show the error in terminal

echo $? is a neat way to get the result of previously executed command, 0 when succeeded, and 1 when failed.

Example:

  • ping -c 1 google.com &> /dev/null; echo $? will print out 0 if the ping succeeded.

Another useful thing when debugging a script run with cron is to echo to journal. By echo 'Debug' | /usr/bin/systemd-cat we can see the echoed line in journalctl -f. And do not to forget to add #!/bin/bash at the beginning of the bash script file.

ref



blog comments powered by Disqus