sabato 17 marzo 2012

Unix sample shell script - basic functions

esercise 1

#!/bin/bash

##programma esercitazione unix
echo "inseriti parametri numero parametri $#"

echo "prova esecuzione comando"

returncommand=$(ls | xargs )

echo " comando returncommand = $returncommand"

echo "exit code del comando =  $?"


echo "test ciclo for"
 for i
  do
    echo " $i"
  done


echo "test chiamata funzione"

printHello () {
 for j
  do
   echo "$j"
  done

}


printHello hello1 hello2 hello3


echo " test condizionali "

if [[   -n $returncommand ]]
then
  echo "variabile c'e'"
fi


if [[  -n vavavavav  ]]

then
  echo "secondo me va'"
fi

if [[ -n   $adesso_incazza ]]
then
  echo "qui non entra "
else
 echo "variable missing "
fi



echo "operands"

if [[ 1   -gt 0 ]]
then
   echo " 1 > 0 is true "
fi

echo "uguaglianza stringhe"


mystring="jdjdjdjdjjdjdjd"

if [[ "$mystring"  == "jdjdjdjdjjdjdjd"  ]]
then
 echo "so proprio uguali"
fi


echo "piccolo array"

array=(valore1 valore2 valore3);


for k in 0  1 2
do
  echo "   ${array[$k]}"
done



esercise 2 :

#!/bin/bash

##funzione di test argomenti
checkArgs () {
 if [[ -n  $1   ]]
  echo "variabile  $1   trovata "
  return 0
 then
  echo "variabile $1 non trovata "
  return 1
 fi

}

checkUser () {
 echo "$1"
 if [[  -n  "$1"   ]]
 then
 user=$( whoami | grep "$1" )
 if [[ "$user"  == "$1"     ]] 
 then
   echo "good user"
   return 0
 else
  echo "bad user"
  return 1
 fi
 else
   echo "user not passed "
   return 1
 fi


}




checkUser root 

#################################################

Other common usage commands examples

find +  exec  find . -name 'pippo.sh' -exec ls {} \;  Notare spaziatura tra parentesi e \ e l'uso di {} come place holder

 find  exec scp find . -name 'pippo.sh' -exec scp {} root@localhost:/usr  \; Nota che l'scp viene fatto n volte per quanti sono i file trovati dal find
 Nota che l'scp non funziona con xargs

find + xargs

 find . -name 'pippo.*' | xargs  ls
 find . -name 'pippo.*' | xargs  tar -cvf tarone.tar  | scp tarone.tar  root@localhost:/usr/


find $jboss_home/server/default/conf/servprov ! -name "spro_multiolb_issw_list.xml" ! -name "spro_adasif_properties.xml" ! -name "spro_properties.xml" | xargs -i cp -r {} $jboss_home/server/default/repository/links/servprov

Interactive installation script logging -
Use script command:
script -c ./installation_script.sh installation_log.txt

For session logging type script <logfilename>  and <ctrl+D> to stop logging

Xml file well formed check
To test if an xml file is wekll formed use:
xmllint <file_name>
If your xml file is well formed xmllint shows all file otherwise blocks with a parse error.

Check if program is running with bash shell script

 ps -ef | grep '[j]boss-4.2.3GA'
or
 ps -ef | grep   -v grep | grep   'jboss-4.2.3GA'
or
 ps -ef | grep [/j]boss-4.2.3

more on regexp
http://www.cyberciti.biz/faq/grep-regular-expressions/

Nessun commento:

Posta un commento