Convert Strings with Shell Script

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a shell script to convert a given string to either upper case or lower case based on the user's input. We will use the tr command to perform the string conversion. The script will take a filename as input and ask the user to select the conversion option.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/case("`Case Statements`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/cat -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/exit -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/echo -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/pipeline -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/redirect -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/read -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/tr -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/mv -.-> lab-18283{{"`Convert Strings with Shell Script`"}} linux/sudo -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/shebang -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/comments -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/quoting -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/variables_usage -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/case -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/cond_expr -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/exit_status -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/read_input -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/cmd_substitution -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/subshells -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/adv_redirection -.-> lab-18283{{"`Convert Strings with Shell Script`"}} shell/globbing_expansion -.-> lab-18283{{"`Convert Strings with Shell Script`"}} end

String Conversion

In this challenge, you are tasked with writing a script that converts a string from lower case to upper case and from upper case to lower case based on user selection.

Tasks

You need to complete the following task:

  1. Write a script that converts the input string to upper case or lower case based on user selection.

Requirements

You must meet the following requirements:

  • Use the tr command for string conversion.
  • The script should be named stringConv.sh.
  • Use the working directory ~/project.
  • Provide a filename through command-line input.
  • Prompt the user for the conversion from lower to upper or upper to lower.

Example

labex:project/ $ bash stringConv.sh input_file.txt

[INPUT] Please Select an option [INPUT]
1.TO_UPPER_CASE
2.to_lower_case
1
[OUTPUT] Content of Input File AFTER OPERATION [OUTPUT]
THIS PROGRAM TAKES AN INPUT_FILE AS AN INPUT
AND CONVERTS THE STRING INTO UPPER_CASE
AND LOWER_CASE
labex:project/ $ bash stringConv.sh input_file.txt

[INPUT] Please Select an option [INPUT]
1.TO_UPPER_CASE
2.to_lower_case
2
[OUTPUT] Content of Input File AFTER OPERATION [OUTPUT]
this program takes an input_file as an input
and converts the string into upper_case
and lower_case

Summary

After completing this challenge, we have learned how to use the tr command to perform string conversion in a shell script. We have also learned how to take user input in a shell script and use it to perform different operations. This challenge has helped us improve our shell scripting skills and given us a better understanding of how to work with strings in a shell script.

Other Linux Tutorials you may like