User Tools

Site Tools


aix:scripts_tips

csv file

reorder member in csv file

# cat $file1 | awk -F';' '{ t = $3 ; $3 = $1; $1 = t; print; }' OFS=';' | sed 's/;/,/g' | awk -F',' '{ t = $2 ; $2 = $1; $1 = t; print; }' OFS=',' | sort > $file1.1

http://blog.fpmurphy.com/2008/10/ksh93-date-manipulation.html#sthash.oZOsKv15.dpbs

line number in a file

Find the line number for a specific pattern

[root@aixnim]/root# cat /etc/resolv.conf | perl -lne 'm|10.10.2.100| && print $.'
2

date conversion on AIX

On AIX you can install coreutils rpm package to convert date:

epoch to date

# /opt/freeware/bin/date -d @126507600
Fri Jan  4 06:00:00  1974

Convert date to epoch

# /opt/freeware/bin/date -d "UTC 1970-01-01 126507600 secs" "+%s"
126507600

Add line in ksh AIX

Diffrent commands exists, but sed AIX doesn't allow a option, so we can use perl:

# perl -pe 's/export TMOUT/$&\nFGOLD=\`echo "\\033[1;32m"\`/'

random number

Print a random number between 0 and 51

# let rand=$RANDOM%52
# echo $rand

count

Count number of a word in a line

# echo "aa:bb:cc:dd:ee" | sed 's/[^:]//g' | awk '{ print length }'
4

Suppress multiple blank space in a line / replace by only one blank space

# echo "a  b c     d" | tr -s " "
a b c d

How to remove special characters from files on UNIX

Remove special characters, blank spaces, ^M, ^@, ^I etc. (control M, control I, …)

# sed 's/[^A-Za-z0-9_.;]//g'  file_name1.txt > file_name1.txt

Debug a ksh script:

script.sh | truss -p $$ > /tmp/truss.log 2>&1 

To log less:

truss -f -t !close -p $$ : suppress all close system call to reduce log size, and -f trace also children

trap command

trap 'command' signal_list

# kill -l : to list signals

1) HUP HUP hangup : envoie un signal de réinitialisation
2) INT INT Interruption
3) QUIT QUIT Core dump : génère un fichier core.
4) ILL ILL Le programme tente d'exécuter du code malformé, inconnu ou avec de mauvais privilèges
5) TRAP TRAP Le programme envoie un signal au débugger (message capturé)
6) IOT IOT idem SIGABRT : interruption du programme
7) EMT EMT emulator trap : un programme émulé ou virtualisé a posé problème
8) FPE FPE floating-point exception : le programme a réalisé une opération arithmétique erronée
9) KILL KILL arrête le programme immédiatement
10) BUS BUS Le programme a causé une erreur de bus
11) SEGV SEGV Segmentation fault : le programme fait référence à un mauvais emplacement de mémoire
12) SYS SYS Un mauvais argument est passé en paramètre
13) PIPE PIPE Un programme tente d'écrire dans un pipe sans processus connecté à l'autre bout
14) ALRM ALRM La limite de temps est dépassée
15) TERM TERM Envoie un signal au programme pour le terminer
16) USR1 USR1/USR2 Envoie un signal dans des conditions définies par un utilisateur
17) USR2
18) CHLD CHLD/CLD child : signal envoyé par un programme lorsqu'un processus fils est achevé
19) PWR PWR power : le système subit un problème d'alimentation
20) VTALRM VTALRM virtual alarm : signal envoyé lorsque le temps limite a été dépassé
21) PROF PROF profiler : signal envoyé lorsqu'un timer a expiré
22) POLL POLL polling : un problème est survenu lors d'un événement I/O asynchrone
23) WINCH WINCH window [size] change : signal envoyé au programme lorsque la fenêtre de contrôle change de taille
24) STOP STOP signal demandant au programme de se suspendre
25) TSTP TSTP tty stop : signal envoyé au programme lorsqu'un terminal suspend ses requêtes
26) CONT CONT Redémarre un programme suspendu par STOP
27) TTIN TTIN Le programme tente de lire tty alors qu'il est en arrière-plan
28) TTOU TTOU Le programme tente d'écrire sur tty alors qu'il est en arrière-plan
29) URG URG Un socket a une donnée urgente à lire
30) LOST LOST Le verrou d'un fichier a été perdu
31) RESERVED
32) DIL
33) XCPU XCPU Le programme a utilisé le CPU prend trop longtemps
34) XFSZ XFSZ Le fichier a dépassé la taille maximale autorisée
35) bad trap
36) bad trap
37) RTMIN RTMIN/RTMIN+n real-time minimum : signaux définis par l'application
38) RTMIN+1
39) RTMIN+2
40) RTMIN+3
41) RTMAX-3 RTMAX/RTMAX-n real-time maximum : signaux définis par l'application
42) RTMAX-2
43) RTMAX-1
44) RTMAX

List file permissions in octal

If you have the rpm package installed : coreutils

root@aixtest /etc> /opt/freeware/bin/stat -c "%a %n" /var/adm/wtmp.1.gz
664 /var/adm/wtmp.1.gz
root@aixtest /etc> /opt/freeware/bin/stat -c "%a %n" /var
755 /var

To be able to apply permissions on files, first save the permissions in octal format, it will be easier to recover all permissions:

ls -l | awk 'BEGIN {
   v["r1"] = 400; v["w2"] = 200; v["x3"] = 100; v["s3"] = 4100; v["S3"] = 4000;
   v["r4"] = 40 ; v["w5"] = 20 ; v["x6"] = 10 ; v["s6"] = 2010; v["S6"] = 2000;
   v["r7"] = 4  ; v["w8"] = 2  ; v["x9"] = 1  ; v["t9"] = 1001; v["T9"] = 1000;
}
NF <= 2 { print $0 }
NF > 2 { val = 0; xa = " ";
   for (i = 1; i <= 9; i++) val += v[substr($1, i+1, 1)i];
   if (substr($1, 11, 1) == "+") xa = "+";
   printf " %04d%c %s\n", val, xa, $0;
}'

Or more light:

ls -l | awk 'BEGIN {
v["r1"]=400; v["w2"]=200; v["x3"]=100; v["s3"]=4100; v["S3"]=4000
v["r4"]=40 ; v["w5"]=20 ; v["x6"]=10 ; v["s6"]=2010; v["S6"]=2000
v["r7"]=4  ; v["w8"]=2  ; v["x9"]=1  ; v["t9"]=1001; v["T9"]=1000}
{val=0
 for (i=1;i<=9;i++) val=val+v[substr($0,i+1,1)i]
 printf "%4d %s\n",val,$NF}'

Split full file in directory and file name

a='/var/spool/cron/crontabs/root'
dir=${a%/*}
file=${a##*/}

Last occurence of a word

Use the “rev” reverse command so you can suppress the first occurence and the reverse again:

For example remove the version string from rpm packages: Before:

root@nimbcp - /root/scripts > rpm -qa
zip-3.0-1
unzip-6.0-2
bzip2-1.0.6-1
openldap-2.4.23-0.3
ganglia-gmond-python-examples-3.6.0-2
ganglia-mod_aixdisk-3.6.0-2
root@nimbcp - /root/scripts > rpm -qa | rev | cut -d'-' -f3- | rev
zip
unzip
bzip2
openldap
ganglia-gmond-python-examples
ganglia-mod_aixdisk

Update rpm package to latest version

If you have a big directory with serval versions of the same rpm, you can upgrade only the packages that are installed, just exclude source packages:

root@nimbcp - /export/other/rpm # for pkgi in $(rpm -qa | sort)
do
  pkgs=$(echo $pkgi | rev | cut -d'-' -f3- | rev)
  if [ $(ls ${pkgs}-[0-9]* > /dev/null 2>&1;echo $?) -eq 0 ]
  then
    pkgn=$(ls ${pkgs}-[0-9]* | sort | grep -v ".src." | tail -1)
    if [ $(echo $pkgn | grep -v $pkgi > /dev/null 2>&1;echo $?) -eq 0 ]
    then
      echo rpm -Uhv $pkgn
    fi
  fi
done

Array on ksh93

Define an array

# myarray=(a b c)

Add an element to the array

# myarray+=(a)

List nb of elements, and list elements

# echo ${#myarray[@]}
4
# echo ${myarray[*]}
a b c a

List all elements one by one

nb=${#myarray[@]}
for ((i=0; i<nb;i++))
do
  echo ${myarray[$i]} 
done
aix/scripts_tips.txt · Last modified: 2023/06/07 17:38 by manu