noviceSam
(Samuel Adams)
August 21, 2021, 4:59pm
1
I ran the following script:
#!/bin/bash
DATE = date
echo “Today’s date is $DATE.”
USER = 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???
1 Like
kovacslt
(László Kovács)
August 21, 2021, 7:29pm
2
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
variable=$(command)
That’s why
DATE=$(date)
USER=$(who | wc -l)
Would make much more sense, I think.
Oh, almost forgot it: welcome to the forum!!!
2 Likes
noviceSam
(Samuel Adams)
August 21, 2021, 7:48pm
3
Excellent…that worked a treat!!! Thank you for your help and kind reply…and for the welcome to the forum
1 Like
Akito
August 21, 2021, 8:38pm
4
Using backticks for executing anything is deprecated since forever.
2 Likes
TrekJunky
(Cane Kostovski)
August 21, 2021, 8:52pm
5
how do you list the users logged in? with “who”?
noviceSam
(Samuel Adams)
August 21, 2021, 10:03pm
6
I appreciate that Akito…I was truly baffled because I was following an exercise from a “reputable” training provider.
noviceSam
(Samuel Adams)
August 21, 2021, 10:04pm
7
I simply type “who” and hit enter. Alternatively, I believe you can also type “w” which will give some added detail.
Hope this helps.
1 Like
Akito
August 21, 2021, 10:06pm
8
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”.
1 Like
TrekJunky
(Cane Kostovski)
August 22, 2021, 2:33am
9
thanks for the info. i tried w and who. they both work.
1 Like
kovacslt
(László Kovács)
August 22, 2021, 4:44am
10
In the OP I didn’t see backticks. Did I miss something?
Pretty sure that’s discourse reading those backtick characters as markdown/markup…
noviceSam
(Samuel Adams)
August 22, 2021, 11:26am
12
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 ??
1 Like