Difference between revisions of "Bash examples"

From Teknologisk videncenter
Jump to: navigation, search
m
m
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Linux]][[Category:UNIX]]
+
[[Category:Linux]][[Category:UNIX]][[Category:bash]]
__TOC__
+
{{TOCright}}
 
{{#css:
 
{{#css:
 
      
 
      
     pre {  font-weight: bold; font-size: 150%;}
+
     pre {  font-weight: bold; font-size: 100%;}
 
}}
 
}}
 
= bash builtins =
 
= bash builtins =
Line 99: Line 99:
 
   echo -e "....[DONE]"
 
   echo -e "....[DONE]"
 
done
 
done
 +
</source>
 +
</div>
 +
== Menu eksempel ==
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
#!/usr/bin/bash
 +
#
 +
# Dette er en kommentarlinier
 +
#
 +
# Bla Bla
 +
#
 +
########
 +
 +
 +
LOOP="yes"
 +
 +
while test $LOOP == "yes"
 +
do
 +
 +
  clear
 +
  echo -en " BRUGER MENU:
 +
 +
        1: Online brugere.
 +
        2: Diskplads brugere
 +
        3:
 +
 +
        -: Afslut
 +
 +
  Indtast valg: "
 +
 +
  read VALG
 +
 +
  case $VALG in
 +
    1) clear
 +
      echo -en "          B R U G E R E    L O G G E T  I N D "
 +
      finger | less -P"Tast q for at afslutte"
 +
      ;;
 +
    2) clear
 +
      echo -e "            DISKPLADS  FORBRUG  BRUGERBASERET"
 +
      echo -e "            =================================\n"
 +
      cd /home
 +
      du -ks * 2>/dev/null | sort -rn | less -P"Tast q for at afslutte"
 +
      ;;
 +
    3) clear
 +
      echo -e "Dette menupunkt er ikke udviklet endnu"
 +
      echo -en "\n Tast <ENTER> for at fortsaette."
 +
      read DUMMY
 +
 +
      ;;
 +
    -) LOOP=no
 +
      ;;
 +
    *) echo "\x07"
 +
      ;;
 +
  esac
 +
done
 +
</source>
 +
</div>
 +
=== Menuopstart automatisk ved login ===
 +
 +
Skriv i filen ''.bash_profile'' i dit hjemmebibliotek
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
[heth@bsd ~]$ cd
 +
[heth@bsd ~]$ cat .bash_profile
 +
/home/heth/bin/minmenu
 +
#exit
 +
</source>
 +
</div>
 +
 +
== Checkdisk hver time ==
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
 +
#!/usr/bin/bash
 +
 +
## Find procentsatsterne fra df kommandoen
 +
PROCENT=`df | grep -v devfs | grep -v Filesystem | cut -c 42-44`
 +
 +
for DREV in $PROCENT
 +
do
 +
  if test $DREV -ge 90
 +
  then
 +
    echo -e "Et af drevene ernaesten fyldt\n\n`df`\n\nHilsen\nFreeBSD"\
 +
| mail -s "ATTENTION: drev fyldt paa $HOSTNAME" heth@mercantec.dk
 +
  fi
 +
done
 +
</source>
 +
</div>
 +
=== Crontab filen ===
 +
Start med kommandoen ''crontab -e''
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
#Linjer der starter med '#' er kommentar linier
 +
# * * * * *  /usr/bin/program -l 23
 +
# | | | | |
 +
# | | | | |
 +
# | | | | ------> Ugedag (0 = søndag.....6 = lørdag)
 +
# | | | --------> Måned (1 til 12)
 +
# | | ----------> dato  (1 til 31)
 +
# | ------------> time  (0 til 23)
 +
# --------------> minut (0 til 59)
 +
##################################################
 +
# Programmet slettmp kører hver søndag kl. 23:00
 +
#0 23 * * 0 /usr/bin/slettmp
 +
1,53 * * * * /home/heth/bin/checkdisk
 +
</source>
 +
</div>
 +
== Automatisk login/password med ssh og scp ==
 +
For at kopiere filer fra maskine '''fra.dk''' til maskine '''til.dk''' skal følgende procedure følges. Jeg er bruger '''heth'''
 +
=== Handling på maskine fra.dk ===
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
cd
 +
ssh-keygen -t dsa
 +
scp id_dsa.pub  heth@til.dk:/home/heth/.ssh/authorized_keys2
 +
</source>
 +
</div>
 +
=== Handling på maskine til.dk ===
 +
<div style="margin: 10px 100px">
 +
<source lang="bash">
 +
chmod 644 /home/heth/.ssh/authorized_keys2
 
</source>
 
</source>
 
</div>
 
</div>

Latest revision as of 18:15, 28 February 2010

bash builtins

if command

Greetings

#!/bin/bash
#Uncomment HOUR=`date +%H` when using for real
#HOUR=`date +%H`
#
#HOUR=$1 only for testing purposes.....
HOUR=$1
if test $HOUR -le 7
  then
    echo "You are early go home"
  else if test $HOUR -le 8
         then
           echo "You are early"
         else if test $HOUR -lt 12
                then
                  echo "Good morning, Vietnam"
                else if test $HOUR -lt 16
                       then
                         echo "Good evening"
                     fi
              fi
       fi
fi

while

count example

#!/bin/bash
COU=1
while test $COU -lt 10000
do
  echo -en "COU = $COU\r"
  COU=`expr $COU + 1`
done

case

secret password example

#!/bin/bash
# Read the username from the terminal
echo -en "Enter username: "
read USERNAME
# Read the password from the terminal not echoing the characters
echo -en "Enter password: "
stty -echo
read PASSWORD
stty echo
# Find the username in the case and check password
case $USERNAME in
  john|John) if test $PASSWORD = "banana"
               then
                echo "Access granted"
               else
                echo "Access denied...."
                exit
             fi
           ;;
  eve|Eve) if test $PASSWORD = "apple"
               then
                echo "Access granted"
               else
                echo "Access denied...."
                exit
             fi
           ;;
  *)       echo "Access denied...."
           exit
           ;;
esac
echo "The secret information is 198273"

for

"ADD" users example

#!/bin/bash
USERLIST="bob eve john henry george jane ursula"
for USER in $USERLIST
do
  echo -en "Creating user $USER"
  # Next three line just pretend to add a user. Just playing :-)
  echo "user $USER login permitted" >> /tmp/allowedUsers
  sleep 1
  echo -e "....[DONE]"
done

Menu eksempel

#!/usr/bin/bash
#
# Dette er en kommentarlinier
#
# Bla Bla
#
########


LOOP="yes"

while test $LOOP == "yes"
do

  clear
  echo -en " BRUGER MENU:

         1: Online brugere.
         2: Diskplads brugere
         3:

         -: Afslut

   Indtast valg: "

  read VALG

  case $VALG in
    1) clear
       echo -en "           B R U G E R E    L O G G E T   I N D "
       finger | less -P"Tast q for at afslutte"
       ;;
    2) clear
       echo -e "            DISKPLADS  FORBRUG  BRUGERBASERET"
       echo -e "            =================================\n"
       cd /home
       du -ks * 2>/dev/null | sort -rn | less -P"Tast q for at afslutte"
       ;;
    3) clear
       echo -e "Dette menupunkt er ikke udviklet endnu"
       echo -en "\n Tast <ENTER> for at fortsaette."
       read DUMMY

       ;;
    -) LOOP=no
       ;;
    *) echo "\x07"
       ;;
  esac
done

Menuopstart automatisk ved login

Skriv i filen .bash_profile i dit hjemmebibliotek

[heth@bsd ~]$ cd
[heth@bsd ~]$ cat .bash_profile
/home/heth/bin/minmenu
#exit

Checkdisk hver time

#!/usr/bin/bash

## Find procentsatsterne fra df kommandoen
PROCENT=`df | grep -v devfs | grep -v Filesystem | cut -c 42-44`

for DREV in $PROCENT
do
  if test $DREV -ge 90
  then
    echo -e "Et af drevene ernaesten fyldt\n\n`df`\n\nHilsen\nFreeBSD"\
 | mail -s "ATTENTION: drev fyldt paa $HOSTNAME" heth@mercantec.dk
  fi
done

Crontab filen

Start med kommandoen crontab -e

#Linjer der starter med '#' er kommentar linier
# * * * * *   /usr/bin/program -l 23
# | | | | |
# | | | | |
# | | | | ------> Ugedag (0 = søndag.....6 = lørdag)
# | | | --------> Måned (1 til 12)
# | | ----------> dato  (1 til 31)
# | ------------> time  (0 til 23)
# --------------> minut (0 til 59)
##################################################
# Programmet slettmp kører hver søndag kl. 23:00
#0 23 * * 0 /usr/bin/slettmp
1,53 * * * * /home/heth/bin/checkdisk

Automatisk login/password med ssh og scp

For at kopiere filer fra maskine fra.dk til maskine til.dk skal følgende procedure følges. Jeg er bruger heth

Handling på maskine fra.dk

cd
ssh-keygen -t dsa
scp id_dsa.pub  heth@til.dk:/home/heth/.ssh/authorized_keys2

Handling på maskine til.dk

chmod 644 /home/heth/.ssh/authorized_keys2