0

I use 5 Raspberry Ps on a LAN as a hobby.
Plan to use scripts created on one of the Pis on the other four
However the following script works as expected on the PI where it was created (i.e outputs a file called HOSTNAME.txt

#!/bin/sh
ps -alx | awk '{print $13}' > $HOSTNAME.txt
wc $HOSTNAME.txt

But fails on any other PI: the output file is called ".txt" instead of "HOSTNAME.txt"
I have checked the permissions on the script file
and also the output of echo HOSTNAME which is as expected when run standalone but empty when called in the script.

New contributor
MikeDaFish is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • When you use $HOSTNAME in your script, it tries to use the value of the environment variable HOSTNAME. If this variable is not set or empty on the other Pis, you get an empty string, resulting in the output file being named .txt.
    – liaifat85
    Commented Jun 11 at 12:26
  • 1
    Welcome -- but basic shell programming questions belong on Unix & Linux or Stack Overflow. In this case: "the output ofecho $HOSTNAME which is as expected when run standalone but empty when called in the script" points to the issue indicated in the previous comment. Which should allow you to re-ask the question (in a more appropriate place) with a more precise problem -- a clue to which is the difference between bash (the default CLI shell) and sh (which you use in your shebang).
    – goldilocks
    Commented Jun 11 at 13:55

0

Browse other questions tagged or ask your own question.