Bash scripting

 

Numerical Comparison Operators for the test Command

Operator   Description
int1 -eq int2  True if int1 equals int2.
int1 -ne int2  True if int1 is not equal to int2.
int1 -lt int2  True if int1 is less than int2.
int1 -le int2  True if int1 is less than or equal to int2.
int1 -gt int2  True if int1 is greater than int2.
int1 -ge int2  True if int1 is greater than or equal to int2.


Special Shell Variables
Variable  Description

$0    The name of the command being executed. For shell scripts, this is the path with which it was invoked.
$n    These variables correspond to the arguments with which a script was invoked.
    Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1,
    the second argument is $2, and so on).
$#    The number of arguments supplied to a script.
$*    All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@    All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$?    The exit status of the last command executed.
$$    The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
$!    The process number of the last background command.

===============================================================================

voorbeeld   . ./disscript.sh

met disscript :

 #!/bin/bash
 echo "DISPLAY=10.1.12.$1:0.0"
 export DISPLAY=10.1.12.$1:0.0

.  filename [arguments]
  source filename [arguments]
         Read  and  execute commands from filename in the current shell environment and return the exit
         status of the last command executed from filename.

===============================================================================

Automatisch uitvoeren van taken in Rtap

/etc/cron.hourly/logrotate.nsisc

#!/bin/bash
if [[ "`date +%H`" = "00" ]] ; then
  logrotate -f /project/ns/config/LogCfg
else
  logrotate /project/ns/config/LogCfg
fi

===============================================================================

Delete lines from text file met een bepaald pattern.

1. creer een script :

replace_str.sh

[ns@V-RHEL5-RTAP poo008002002]$ cat replace_str.sh
#!/bin/bash
S2REP1="Type = rtSCALAR"
S2REP2="De Type = rtUINT8"
S2REP3="Read Groups = 1111"
S2REP4="Write Groups = 1111"
S2REP5="BEGIN ATTRIBUTE"
S2REP6="END"

REPLACE_STRING=""

set $(find ./$1 -type f -exec grep -lI "$S2REP1"|"$S2REP2"|"$S2REP3"|"$S2REP4"|"$S2REP5"|"$S2REP6" '{}' \;)
for file in "$@"
do
  sed -e "s|$S2REP1|$REPLACE_STRING|g" -e "s|$S2REP2|$REPLACE_STRING|g" -e "s|$S2REP3|$REPLACE_STRING|g" -e "s|$S2REP4|$REPLACE_STRING|g" -e "s|$S2REP5|$REPLACE_STRING|g" -e "s|$S2REP6|$REPLACE_STRING|g" $file > /tmp/tmpfile
  mv /tmp/tmpfile $file
done

2.

verwijder al de lijnen uit de text file "poo008002002" met een bepaald patroon "De Type ="

sed "/De Type =/"d poo008002002 > tmp

OPM.

1,2 combineren levert dan het volgende script op :

#!/bin/bash
S2REP1="Type = rtSCALAR"
S2REP2="De Type = rtUINT8"
S2REP3="Read Groups = 1111"
S2REP4="Write Groups = 1111"
S2REP5="BEGIN ATTRIBUTE"
S2REP6="END"
S2REP7="De Type ="
REPLACE_STRING=""

set $(find ./$1 -type f -exec grep -lI "$S2REP1"|"$S2REP2"|"$S2REP3"|"$S2REP4"|"$S2REP5"|"$S2REP6"|"$S2REP7" '{}' \;)
for file in "$@"
do
  sed -e "s|$S2REP1|$REPLACE_STRING|g" -e "s|$S2REP2|$REPLACE_STRING|g" -e "s|$S2REP3|$REPLACE_STRING|g" -e "s|$S2REP4|$REPLACE_STRING|g" -e "s|$S2REP5|$REPLACE_STRING|g" -e "s|$S2REP6|$REPLACE_STRING|g" -e "/De Type =/"d $file > /tmp/tmpfile
  mv /tmp/tmpfile $file
done


3 verwijderen van de lege regels:

using sed

$ sed '/^$/d' input.txt > output.txt

using grep

$ grep -v '^$' input.txt > output.txt

===============================================================================

veranderen van tekst in linux folders

replace.sh {:poortbediening_uit:mainFrame:tx_pw} {:$filename:mainFrame:tx_pw} `find .\ -name poo*`

replace script:
----------------

#!/bin/bash
STRING_TO_REPLACE="\-O3"
REPLACE_STRING="\-O2"

set $(find ./src -type f -exec grep -lI "\-O3" '{}' \;)
for file in "$@"
do
  sed "s|$STRING_TO_REPLACE|$REPLACE_STRING|g" $file > ./tmp2/tmpfile
  mv ./tmp2/tmpfile $file
done

===============================================================================

if options :

 for i in /etc/rc$runlevel.d/K*; do
    # Check if the script is there.
    [ ! -f $i ] && continue
 done

===============================================================================

 

===============================================================================

 

===============================================================================