Categories: How-To

Linux case command (compare values) [Basic Guide]

The case command in Linux is an intelligent way to compare the value of something with various standards.

If an occurrence is positive, it allows commands to be executed.

Each case is an expression that corresponds to a pattern.

The “)” operator ends a list of patterns and starts a list of commands.

What separates one pattern from another is “;;”.

At the end of the case, you must finish with the esac instruction (case on the contrary).

Example of a script that, depending on the user’s UID, prints a different message:

#! /bin/bash 
ID=$ (id -u)
case “$ID” in
0)
echo “You are the root.” ;
echo “Congratulations!”
;;
1000)
echo “You are Uira.”

;;
100 [1-9])
echo “You are another user”
esac
echo “end”;

If the value of the ID variable is 0, it will print a congratulations message. If it is equal to 1000, print “you are Uira”. If it’s between 1001 and 1009, print “You’re another user.

If executed with the user uira, who has a UID equal to 1000:

$. /meucase 
You are Uira.

End

If run as root:

$ I sweat. /mycase 
You are root.
Congratulations!

End

The case is better than using the “if” command several times to compare something with various patterns.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.