BASH Snippets

Test if file exists

#!/bin/bash
 
configfile='ca_config.txt'
 
if [ -e "${configfile}" ]; then
        echo "${configfile} exists."
else
        echo "${configfile} not exists."
        exit 2
fi

Case Statement

#!/bin/bash
 
printf "Select duration: \n1: Choice 1\n2: Choice 2\n3: Choice 3\n"
 
printf "Choice 1, 2 or 3:- "
read Choice
 
case "${Choice}" in
 
  1)
    echo -n 'One'
    Type='One'
    ;;
 
  2)
    echo -n 'Two'
    Type='Two'
    ;;
 
  3)
    echo -n 'Three'
    Type='Three'
    ;;
 
  *)
    echo -n 'Invalid choice, select either 1, 2 or 3.'
    ;;
esac
 
printf "Choice ${Type}\n"
 
linux/bash-snippets.txt · Last modified: 13/02/2024 09:55 by andrew