1
#!/bin/bash
read n
sd=0
rev=0

if[[ $n -lt 0 ]];
then
echo "Not a positive number"
else
while [[ $n -gt 0 ]]
do
sd=$(( $n % 10 ))
rev=`expr $rev \* 10 + $sd`
n=$(( $n /10 ))
done

echo $rev

./scriptprog4.sh: line 6: if[[ 123 -lt 0 ]]: command not found ./scriptprog4.sh: line 7: syntax error near unexpected token then' ./scriptprog4.sh: line 7:then'

1
  • 1
    Consider running your code through shellcheck.net before asking questions here. Commented May 13, 2020 at 15:48

2 Answers 2

1

There was a missing fi statement (highly recommended to indent the code). Here's a working script:

#!/bin/bash
read n
sd=0
rev=0

if [[ $n -lt 0 ]]
then
    echo "Not a positive number"
else
    while [[ $n -gt 0 ]]
    do
        sd=$(( $n % 10 ))
        rev=`expr $rev \* 10 + $sd`
        n=$(( $n /10 ))
    done
fi

echo $rev

and an execution:

$ ./scriptprog4.sh 
123
321
Sign up to request clarification or add additional context in comments.

1 Comment

Note that expr is not good practice. It's a holdout from the 1970s; any shell compatible with the 1992 POSIX sh specification supports rev=$(( rev * 10 + sd )), which is both guaranteed to be available on any standard-compliant shell (expr is not), and more efficient (being built into the shell itself, as opposed to an external process).
0

./scriptprog4.sh: line 6: if[[ 123 -lt 0 ]]: command not found

is caused by a missing space between if and [[ both are reserved words in bash and must be separated by space

if [[ $n -lt 0 ]]; then

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.