0

I wrote the following code in shell script:

#!/bin/bash
tput clear
a=$(date +"%k")
if [ $a -lt 12 ]
then
echo "Hi!Good Morning"
fi
if [ $a -ge 12 -a $a -le 17 ]
then
echo "Hi!Good Afternoon"
fi
if [ $a -gt 17 -a $a -le 19 ]
then
echo "Hi!Good Evening"
fi
if [ $a -gt 19 -a $a -le 24 ]
then
echo "Hi!Good Night"
fi
while [ : ]
do

    echo "BCSE!!\c" 
    read comm
    set comm
    case "$1" in
        [""])
            continue
        ;;
    esac
    case "$1" in
        ["editme"])
            xdg-open "$2"& 
        ;;
    esac
        case "$1" in
        ["newd"])
            mkdir -p "$2"
        ;;
    esac
    case "$1" in
        ["mycontent"])
            if [ -f "$2" ]
            then
                xdg-open "$2"&
            else
                echo "File doesn't exist"
            fi
        ;;
    esac
    case "$1" in
        ["exitbcse"])
                break
        ;;
    esac
    case "$1" in
        [*])
            echo "Wrong command!!";;
    esac
done

The output should be :

Hi!Good morning
BCSE!!editme filename 

now the file doesn't open instead I get

Hi!Good morning
BCSE!!editme filename
BCSE!!

1 Answer 1

1

Instead of:

while [ : ]

You may want to write:

while :

or

while true

while [ : ] may work, but not for the right cause, that sentence runs the command [, the command [ checks the expression you wrote inside, as it is a non-empty string it returns a true value (a zero), to ilustrate this, if you run while [ false ] you will also get an infinite loop.

And in the case control structures the options must be written without [] and "". case "$1" in exitbcse) break ;; esac

Edit:

Check this example with the corrections I described above and also other fixes:

#!/bin/bash
tput clear
a=$(date +"%k")
if [ $a -lt 12 ]
then
    echo "Hi!Good Morning"
elif [ $a -ge 12 -a $a -le 17 ]
then
    echo "Hi!Good Afternoon"
elif [ $a -gt 17 -a $a -le 19 ]
then
    echo "Hi!Good Evening"
elif [ $a -gt 19 -a $a -le 24 ]
then
    echo "Hi!Good Night"
fi
while true
do
    echo "BCSE!!\c" 
    read comm option
    case "$comm" in
        "")
            continue
            ;;
        "editme")
            xdg-open "$option"& 
            ;;
        "newd")
            mkdir -p "$option"
            ;;
        "mycontent")
            if [ -f "$option" ]
            then
                xdg-open "$option"&
            else
                echo "File doesn't exist"
            fi
            ;;
        "exitbcse")
            break
            ;;
        *)
            echo "Wrong command!!";;
    esac
done
Sign up to request clarification or add additional context in comments.

5 Comments

!Thanks!!But it gives output wrong command everytime.If i remove case $1 in *) then it goes on give the same output.Why does the other cases not execute?
$1 is the first argument you pass to the script, if you want to use the value you have read from keyboard use case "$comm", also the command set in this script has no effect, you can skip it.
Yes it works for exitbcse case.But the other cases do not work.I have changed $1 to $comm
Check the last edit I did, in the last if in order to check if a file exists you must use the [ command (yes it is a command, you can call it test if you want, for example if test -f "$option").
@Fernado -Yes i know file check and yeah your code helped me to finish it.It worked!

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.