Skip to content

Instantly share code, notes, and snippets.

@Guitsou
Last active December 17, 2015 10:49
Show Gist options
  • Save Guitsou/5597877 to your computer and use it in GitHub Desktop.
Save Guitsou/5597877 to your computer and use it in GitHub Desktop.
Exemple de if sous Bash

Simple

Simple

#!/bin/bash
if [ "foo" = "foo" ]; then
    echo expression evaluated as true
fi

Complet

#!/bin/bash
if [ "foo" = "foo" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi

Avec des variables

#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi

Tests

Gender...

gender="male"
if [[ "$gender" == "f*" ]]; then 
    echo "Pleasure to meet you, Madame."
else 
    echo "How come the lady hasn't got a drink yet?"
fi
#How come the lady hasn't got a drink yet?

User local ?

if ! grep ^$USER /etc/passwd 1> /dev/null; then 
    echo "your user account is not managed locally"
else 
    echo "your account is managed from the local /etc/passwd file"
fi
## your account is managed from the local /etc/passwd file

Un autre

cat weight.sh
#!/bin/bash

# This script prints a message about your weight if you give it your
# weight in kilos and height in centimeters.

if [ ! $# == 2 ]; then
  echo "Usage: $0 weight_in_kilos length_in_centimeters"
  exit
fi

weight="$1"
height="$2"
idealweight=$[$height - 110]

if [ $weight -le $idealweight ] ; then
  echo "You should eat a bit more fat."
else
  echo "You should eat a bit more fruit."
fi

Fichier existe ?

#!/bin/bash

# This script gives information about a file.

FILENAME="$1"

echo "Properties for $FILENAME:"

if [ -f $FILENAME ]; then
  echo "Size is $(ls -lh $FILENAME | awk '{ print $5 }')"
  echo "Type is $(file $FILENAME | cut -d":" -f2 -)"
  echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"
  echo "$(df -h $FILENAME | grep -v Mounted | awk '{ print "On",$1", \
which is mounted as the",$6,"partition."}')"
else
  echo "File does not exist."
fi

###Sup des espaces disques

disktest.sh
#!/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 -`
alertvalue="80"

if [ "$space" -ge "$alertvalue" ]; then
  echo "At least one of my disks is nearly full!" | mail -s "daily diskcheck" root
else
  echo "Disk space normal" | mail -s "daily diskcheck" root
fi

###Années bisextiles 1/2

testleap.sh
#!/bin/bash
# This script will test if we're in a leap year or not.

year=`date +%Y`

if [ $[$year % 400] -eq "0" ]; then
  echo "This is a leap year.  February has 29 days."
elif [ $[$year % 4] -eq 0 ]; then
        if [ $[$year % 100] -ne 0 ]; then
          echo "This is a leap year, February has 29 days."
        else
          echo "This is not a leap year.  February has 28 days."
        fi
else
  echo "This is not a leap year.  February has 28 days."
fi

###Années bisextiles 2/2

testleap.sh
#!/bin/bash
# This script will test if we're in a leap year or not.

year=`date +%Y`

if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )); then
    echo "This is a leap year.  February has 29 days."
else
    echo "This is not a leap year.  February has 28 days."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment