In Linux, Converting text to uppercase can be useful in many scenarios, such as when dealing with file names, or when preparing text for printing. In this article, we will explore how to convert text to uppercase using shell programs in Linux. Please copy this shell program so that you can use it when you need. Let’s go through below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/sh/ # convert text of file into upper case echo Enter the filename read filename echo Contents of $filename before converting to uppercase echo ---------------------------------------------------- cat $filename echo ---------------------------------------------------- echo Contents of $filename after converting to uppercase echo --------------------------------------------------- #echo $filename | tr [a-z] [A-Z] tr [a-z] [A-Z] < "$filename" |
Now Let us try to understand each line of the code briefly.
1 |
#!/bin/sh/ |
This is called a shebang, and it specifies the shell that should be used to interpret the script. In this case, it specifies the sh shell.
1 |
# convert text of file into upper case |
This is a comment that explains what the script does.
1 |
echo Enter the filename |
This line prompts the user to enter a filename.
1 |
read filename |
This line reads the filename entered by the user and assigns it to the variable filename.
1 2 |
echo Contents of $filename before converting to uppercase echo ---------------------------------------------------- |
These two lines output a header to indicate the contents of the file before converting to uppercase.
1 |
cat $filename |
This line uses the cat command to display the contents of the file.
1 2 3 |
echo ---------------------------------------------------- echo Contents of $filename after converting to uppercase echo --------------------------------------------------- |
These three lines output a header to indicate the contents of the file after converting to uppercase.
1 2 |
# echo $filename | tr [a-z] [A-Z] tr [a-z] [A-Z] < "$filename" |
These two lines use the tr command to convert the contents of the file to uppercase. The first line is commented out and not used, while the second line actually performs the conversion. The tr command takes two arguments: the first argument specifies the characters to be replaced, and the second argument specifies the characters to replace them with. In this case, it replaces lowercase letters with uppercase letters.
In conclusion, converting text to uppercase in Linux can be done easily using shell programs such as tr. This method will be powerful tools that allow for efficient manipulation of text files in the Linux environment. you can gain a better understanding of how to use this command effectively and efficiently in your own Linux shell scripts. Please check out our another tutorial over linux. COMPARING CONTENTS TWO FILES AND SHOW OUTPUT USING SHELL PROGRAM