Variable lab with printing uptime of the system

Hi folks. Please share a solution for the lab. Thanks

Please share link to the lab so we can easily get to it.

Thanks for the prompt response, Alistair! Here it is Shell Scripts for Beginners | KodeKloud

The issue with this script is that the variable name has an illegal character in it, so is not interpreted as a variable

This is the error

./print-uptime.sh: line 1: up-time=: command not found

You need to make the name of the variable valid in both places it is used.

Review the previous lecture Shell Scripts for Beginners | KodeKloud. You should be able to determine what the issue is from that.

Thanks, Alistair! Could you please share the solution with explanation for this lab question?

In the given script. the first line is written as

up-time=$(uptime)

…and the intention is to assign the output of the linux uptime command. to a variable.

If you review the lecture, you will find that up-time is not a valid name for a variable. Shell thinks that you are trying to run a command called up-time , (because - is valid in file names) and that command does not exist.

You must change all appearances of up-time to up_time to make it valid.

So the rule is

  • Anything that is a valid variable name, immediately followed by ‘=’ with no space is treated as a variable assignment
  • Anything else is treated as a command, and shell will attempt to run it (and fail).

Thanks. No dash in variable is clear. I was confused with a dollar sign. Later I figured we have to use it because “uptime” itself is a command

  • uptime is a real command, documented here, and many other places too.
  • up_time in the correct context is a variable, and with a $ in front is always a variable.
  • up-time the shell thinks you are trying to run a command, but that command doesn’t exist.
1 Like