Home > Software design >  Write a shell script which takes an input (an integer smaller than 10,000) from users and displays e
Write a shell script which takes an input (an integer smaller than 10,000) from users and displays e

Time:03-27

When input “153”, the shell script will identify and display “1”, “5” and “3” on the screen sequentially. If input “69”, it will display “6” and “9”.

CodePudding user response:

Pipe user input through sed:

echo $1 | sed 's/\(.\)/\1 /g'

CodePudding user response:

Bash treats everything as a string.

Here is a Bash way to loop character by character over a string:

s=123
for (( i=0; i<${#s}; i   )); do
    echo "${s:$i:1}"
done

Prints:

1
2
3

Still for you to do:

  1. Determine that s is a valid integer (Hint)
  2. Determine that s is a string representing less than 10,000? (Hint: inside of (( double_parentesis )) Bash treats those strings as integers)

CodePudding user response:

Does your grep support the -o option? Then

$ echo "153" | grep -o .
1
5
3
  • Related