12/10/2022

Shell Scripting

Filed under: — cybrarian @ 10:04 am

(zie ook commandolijn algemeen)

Scripting


$0 variabele: bestandsnaam
bv: mijnscript.sh

$ variabelen 1-9 parameters
bv
ls -lFAh $1*

# commentaar

${ } var scheiden van txt

$# aantal parameters in script
bv:
if [ $# -eq 0 ]; then

pauseren, wachten
sleep 5 (seconden)
sleep 5m (minuten)
sleep 5h (uur)
sleep 5d (dagen)

Quoting:” ‘ `
double or single quotes, backticks
” “ tekst > 1 woord

‘ ‘ tekst letterlijk
uit te voeren commando
|   logische of (or) in case-struc

var=” “ tekst >1 woord toekennen

exit 0 foutwaarde, 0=ok

Let ook:
path nakijken, dir
van uitvoeren script!
in subshell uitvoeren:
script.sh

als root:
./script.sh
geen subshell, sourcen “.”):

. ./script.sh

.inputrc in homedir
/etc/magic bestandsformaten
(naam kan varieren vlgs distr)

Root:
vanuit user :
su root (password)
./script ipv script
(system logs)
tail -f /var/log/messages

            Voorbeelden

Keuze in script:

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make install; break;;
        No ) exit;;
    esac
done

Check of uitgevoerd door root:

# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

ZypperRefUp.sh
Shell script to do updates on an openSUSE system:

(de -n zorgt dat vragen automatisch beantwoord worden)

#!/bin/bash
waitforbreak=40
STARTED=`date`
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root or with sudo " 1>&2
   exit 1
fi
echo "ZypperRefUp.sh [r|s] (optional reboot/shutdown)"
if [ $# -eq 0 ]; then
    echo "Live run with no arguments. If needed CTRL-C and retry. Waiting $waitforbreak sec. ..."
    sleep $waitforbreak
fi
echo "$0 started on $STARTED with parameter $1"
zypper -n ref
zypper -n up
DATE=`date`
echo "Finished $0 at $DATE ">~/ZypperRefUp.log
if [ "$1" == "r" ]
then
  echo "reboot in $waitforbreak s. ..."
  sleep $waitforbreak
  reboot
fi
if [ "$1" == "s" ]
then
  echo "shutdown in $waitforbreak s. ..."
  sleep $waitforbreak
  shutdown -h now
fi
echo "finished $0 on $DATE - Ready."

Install a gambas app after downloading it:
instfromdownload.sh

#/bin/bash
# working on 07/10/2022
# install gambasapp.tar.gz from downloads directory
echo " instfromdownload myGb3App (no extention) to install myGb3App.tar.gz from ~/downloads"
if [ $# -eq 0 ]; then
    echo "- No arguments provided; choose from: "
    ls -lFAh ~/Downloads/*.tar.gz
    echo "or download (manually if no fixed link) from shared nextcloud folder sharekantoor/swupdates/release or other source"
    exit 1
fi
cd ~/Downloads/
ls -lFAh $1*
myapp = $1.tar.gz
echo "move and unpack $myapp"
cp myapp ~/.systemPrograms/gb3Project/
cd ~/.systemPrograms/gb3Project
tar -xzvf myapp
echo "compile install $myapp"
cd myapp
gb3c -a
gba3 -o ./../gb3Run/$myapp.gambas
echo "cleanup .. "
mv ~/.systemPrograms/gb3Source/$myapp /.systemPrograms/gb3Source/previous/
mv /.systemPrograms/gb3Project/$myapp /.systemPrograms/gb3Source/
ls -lFAh /.systemPrograms/gb3Source/$myapp
ls -lFAh /.systemPrograms/gb3Source/$myapp
rm ~/Downloads/$myapp
ls ~/Downloads/$myapp
echo "End install, ready to run $myapp"

Reacties zijn gesloten.

Powered by WordPress