0
    #!/bin/ksh
    echo -n "enter the 1 to convert lower to upper or 2 convert upper to lower"
    read n
    echo -in_str "Enter the string here"
    read in_str
    echo $n
    echo $in_str
    if [ $n -eq 1 ] then
        $ echo $in_str| awk '{print toupper($0)}'
    elif [ -n -eq 2 ] then
        $ echo $in_str| awk '{print tolower($0)}'
    else
        echo "please select the correct choice"
    fi

Getting error: else unexpected i am unable to run the above code

1
  • Alternatives for awk are typeset -u in_str and tr '[:upper:]' '[:lower:]' Commented Sep 8, 2014 at 7:24

3 Answers 3

1

This:

if [ $n -eq 1 ] then

Needs to be this:

if [ $n -eq 1 ]; then

Or this:

if [ $n -eq 1 ]
then
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot guys am very new to unix shell scripting
assuming $n is not blank (or null but normaly not after the read). If not it could be ${n:-9} to assign (if blank/null) to 9 so -eq could be used
1

You need semicolon(s) before then.And you have extra $ symbols. I think you wanted something like this

if [ $n -eq 1 ]; then
    echo $in_str| awk '{print toupper($0)}'
elif [ $n -eq 2 ]; then
    echo $in_str| awk '{print tolower($0)}'
else
    echo "please select the correct choice"
fi

At least it seems to work here.

1 Comment

Actually, I'm pretty certain you need then on the elif as well.
0
$ echo $in_str| awk '{print toupper($0)}'

???

I don't think you want that $ character at the front of that line (or the other echo line either).

In addition, if you want then on the same line as your if, it should be:

if [ $n -eq 1 ] ; then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.