gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
write a bash program that gets two numbers as input, and prints the sum and aso the…
- User
write a bash program that gets two numbers as input, and prints the sum and aso the bigger number . Also, if the user gives inappropiate input, prints error message
- Assistant
Sure, here's a bash program that accomplishes this task:
#!/bin/bash # Prompt the user to enter two numbers read -p "Enter the first number: " num1 read -p "Enter the second number: " num2 # Check if the input is numeric or not if ! [[ $num1 =~ ^[0-9]+$ ]] || ! [[ $num2 =~ ^[0-9]+$ ]]; then echo "Error: Incorrect input! Please enter valid numbers." exit 1 fi # Calculate the sum of the two numbers sum=$((num1 + num2)) echo "Sum: $sum" # Determine the bigger number if (( num1 > num2 )); then echo "Bigger number: $num1" elif (( num2 > num1 )); then echo "Bigger number: $num2" else echo "Both numbers are equal." fiTo run this program, save it to a file (e.g.,
sum_and_bigger.sh), make it executable (chmod +x sum_and_bigger.sh), and run it (./sum_and_bigger.sh).