I ran the following script:
echo “Today’s date is $DATE.”
who | wc -l
echo “There are $USER users currently logged in.”
Whilst the first echo command displayed the expected output, the second displayed the following: ./demo.sh: line 7: 1: command not found.
As a mere novice I am totally confused!!! Could someone please advise???
I see spaces where there should not be, and I see missing brackets and $ signs.
To assign a value to a variable in shell script from an output of a command, you need to do
USER=$(who | wc -l)
Would make much more sense, I think.
Oh, almost forgot it: welcome to the forum!!!
Excellent…that worked a treat!!! Thank you for your help and kind reply…and for the welcome to the forum
Using backticks for executing anything is deprecated since forever.
how do you list the users logged in? with “who”?
I appreciate that Akito…I was truly baffled because I was following an exercise from a “reputable” training provider.
I simply type “who” and hit enter. Alternatively, I believe you can also type “w” which will give some added detail.
Hope this helps.
These are the main reasons I see those backticks all over the place:
- The articles showing them are at least 10 years old.
- Whoever wrote that article is very old and is shell scripting for at least 20 years, but never changed or improved or adjusted their skills in the past 10 years.
- People who “do not care” about deprecated stuff, because “it still works”.
thanks for the info. i tried w and who. they both work.
In the OP I didn’t see backticks. Did I miss something?
Pretty sure that’s discourse reading those backtick characters as markdown/markup…
Forgot to put them
But I had them on the original script I tried to run.
Try using whoami command in simple
Why to use complex commands ??