Getting started with conditionals

Sort by

recency

|

332 Discussions

|

  • + 0 comments

    read -p "y" char

    if [ "char" == "y" ]; then echo "YES" elif [ "char" == "n" ]; then echo "NO" fi

  • + 0 comments
    read x
    
    case $x in
        y|Y) echo "YES" ;;
        n|N) echo "NO" ;;
    esac
    
  • + 0 comments
    read input
    if [[ $input == "y" || $input == "Y" ]]
    then
    echo "YES"
    else
    echo "NO"
    fi
    
  • + 0 comments

    I used case statement ->

    read input case $input in [yY]) echo "YES" ;; [nN]) echo "NO" ;; esac

  • + 0 comments

    Easy solution:

    #!/bin/bash
    
    read CHAR
    
    [ ${#CHAR} -eq 1 ] && [ $CHAR == 'Y' ] || [ $CHAR == 'y' ] && echo "YES" && exit 0
    [ ${#CHAR} -eq 1 ] && [ $CHAR == 'N' ] || [ $CHAR == 'n' ] && echo "NO" && exit 0
    

    EXPLANATION : : we are reading the char, :next if the char == length 1 we pass that to check if it is y,Y,n,N if it is anyone of it we print and exit