gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
In bash: Task Develop a script that takes a string as an argument and returns its…
- User
In bash: Task Develop a script that takes a string as an argument and returns its reverse version, changing uppercase letters to lowercase and back
Check ./loops1.sh “Hello World”
DLROw OLLEh
./loops1.sh “bash”
HSAB
./loops1.sh “EPAM”
mape
- Assistant
Below is a bash script,
loops1.sh, that accomplishes the task of reversing a string, swapping uppercase letters to lowercase and vice versa:bash#!/bin/bash # Check if exactly one argument is provided if [ "$#" -ne 1 ]; then echo "Usage: $0 <string>" exit 1 fi # Read the input string input=$1 # Function to reverse the string reverse_string() { echo "$1" | rev } # Function to swap case of each character swap_case() { echo "$1" | tr 'a-zA-Z' 'A-Za-z' } # Reverse the input string reversed=$(reverse_string "$input") # Swap the case of the reversed string result=$(swap_case "$reversed") # Print the final result echo "$result"To use this script, follow these steps: