• BASH > créer des options pour les scripts

      1. parcourir les options

      for arg in "$@" ; do
         shift
         case "$arg" in
            --nom) echo "bonjour $1"
            ;;
            --age) echo "tu as $1 ans"
            ;;
            --help) echo "tu veux de l'aide."
            ;;
            *) echo "aucune option"
               exit 1
            ;;
         esac
      done
      
      ./monScript.sh --nom "toto" --age 4 --help --hello Hi!
      bonjour toto
      tu as 4 ans
      tu veux de l'aide. 

      VERIFIER SI IL Y A DES ARGUMENTS

      2 arguments

      if [ $# -lt 2 ] ; then
         echo "Usage : $0 Signalnumber PID"
         exit
      fi

       

      case "$1" in
         1) echo "Sending SIGHUP signal"
            kill -SIGHUP $2
         ;;
         2) echo  "Sending SIGINT signal"
            kill -SIGINT $2
         ;;
         3) echo  "Sending SIGQUIT signal"
            kill -SIGQUIT $2
         ;;
         9) echo  "Sending SIGKILL signal"
            kill -SIGKILL $2
         ;;
         *) echo "Signal number $1 is not processed"
         ;;
      esac

       

      voir aussi 4 methods to kill a process.

      2. Pattern Match in a File

      # Check the given file is exist #
      if [ ! -f $3 ] ; then
         echo "Filename given \"$3\" doesn't exist"
         exit
      fi
      
      case "$1" in
         # Count number of lines matches
         -i) echo "Number of lines matches with the pattern $2 :"
            grep -c -i $2 $3
         ;;
         # Count number of words matches
         -c) echo "Number of words matches with the pattern $2 :"
            grep -o -i $2 $3 | wc -l
         ;;
         # print all the matched lines
         -p) echo "Lines matches with the pattern $2 :"
            grep -i $2 $3
         ;;
         # Delete all the lines matches with the pattern
         -d) echo "After deleting the lines matches with the pattern $2 :"
            sed -n "/$2/!p" $3
         ;;
         *) echo "Invalid option"
         ;;
      esac

       

      Voir aussi  ~ expansaion and { } expansion.

      3. File type from the Extension

      #!/bin/bash
      for filename in $(ls) ; do
         # Take extension available in a filename
         ext=${filename##*\.}
         case "$ext" in
            c) echo "$filename : C source file"
            ;;
            o) echo "$filename : Object file"
            ;;
            sh) echo "$filename : Shell script"
            ;;
            txt) echo "$filename : Text file"
            ;;
            *) echo " $filename : Not processed"
            ;;
         esac
      done

      4.  Yes or No

      #!/bin/bash
      
      echo -n "Do you agree with this? [yes or no]: "
      read yno
      case $yno in
         [yY] | [yY][Ee][Ss] )
            echo "Agreed"
         ;;
         [nN] | [n|N][O|o] )
            echo "Not agreed, you can't proceed the installation";
            exit 1
         ;;
         *) echo "Invalid input"
         ;;
      esac

      5. Startup script

      #!/bin/bash
      
      case "$1" in
         'start')
            echo "Starting application"
            /usr/bin/startpc
         ;;
         'stop')
            echo "Stopping application"
            /usr/bin/stoppc
         ;;
         'restart')
            echo "Usage: $0 [start|stop]"
         ;;
      esac
      
      ./startpcapp start
      Starting application
      /usr/bin/startpc started

      6. tester l’espace disque

      #!/bin/bash
      # This script does a very simple test for checking disk space.
      space=`df -h | awk '{print $5}' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`
      case $space in
         [1-6]*) Message="Tout va bien."
         ;;
         [7-8]*) Message="Va falloir penser à faire un peu de ménage : $space % de libre."
          ;;
         9[1-8]) Message="Ouh la! faut faire du ménage, vite : $space % libre."
         ;;
         99) Message="Le disque est rempli !!! $space %"
         ;;
         *) Message="Apparemment, y'a une partition inexistante..."
         ;;
      esac 
      
      echo $Message | mail -s "disk report `date`" monAdresseMail

      VOIR AUSSI

    • 50 Linux Sysadmin Tutorials
    • 50 Most Frequently Used Linux Commands (With Examples)
    • Top 25 Best Linux Performance Monitoring and Debugging Tools
    • Mommy, I found it! – 15 Practical Linux Find Command Examples
    • Linux 101 Hacks 2nd Edition eBook
    • Awk Introduction – 7 Awk Print Examples
    • Advanced Sed Substitution Examples
    • 8 Essential Vim Editor Navigation Fundamentals
    • 25 Most Frequently Used Linux IPTables Rules Examples
    • Turbocharge PuTTY with 12 Powerful Add-Ons
    • Voir aussi : bash if-then-else statement
    • Also, refer to our earlier 15 bash array examples
    • -

 

Aucun commentaire

 

Laissez un commentaire