Visualizzazione post con etichetta shell script. Mostra tutti i post
Visualizzazione post con etichetta shell script. Mostra tutti i post

lunedì 24 giugno 2013

Sorting a file with pipe-delimited fields using unix sort

Hi All

Today a simple command to sort a file with pipe-delimited fields.

My file is :

EG|TW|SA|04/05/2009||
EG|NA|EN|02/05/2003||
SS|TW|SA|04/05/2009||
CD|DC|TI|30/12/2004||
ET|EG|TI|02/05/2003||
NI|SS|EN|18/02/2005||
EG|ET|TI|02/05/2003||
IN|ET|TI|21/09/2000||
SS|SS|SA|28/02/2005||
IN|PR|EN|01/11/2003||
PR|TW|SA|04/05/2009||
SS|EN|TN|18/02/2005||
EG|DC|TI|17/09/2010||
EG|EI|SA|04/08/2005||
NA|IN|SA|21/09/2000||

To sort all records by third field.
I can use these simple command :

sort -t"|" -k 3 [file_name]

Where -t is an option where u can explain field separator and with -k you can explain third field.

Stay tuned.





lunedì 29 aprile 2013

How check if a unix script was killed

Hi all

Today a simple mode to check if a process was killed.

It is adding at the end of your script these two rows :

...
...
...
exitvalue=$?
echo  "exit code of my program is " $exitvalue

When script ends it'll show his exit code.
For example if it was killed (kill -9) appears this message :

exit code of my program is  137

The exit code 137  is 128 + 9 so process ended for a "kill -9" command.

This is a table of HPUX Signal :


       Signal  Value    Action   Comment
       -------------------------------------------------------------------------
       SIGHUP     1             Term    Hangup detected on controlling terminal
             or death of controlling process
       SIGINT     2              Term    Interrupt from keyboard
       SIGQUIT     3              Core    Quit from keyboard
       SIGILL     4              Core    Illegal Instruction
       SIGABRT     6              Core    Abort signal from abort(3)
       SIGFPE     8              Core    Floating point exception
       SIGKILL     9              Term    Kill signal
       SIGSEGV    11     Core    Invalid memory reference
       SIGPIPE    13     Term    Broken pipe: write to pipe with no readers
       SIGALRM    14     Term    Timer signal from alarm(2)
       SIGTERM    15     Term    Termination signal



Stay Tuned.

mercoledì 19 dicembre 2012

Is it possible to pass passwords on a bash shell script?

Yes, it is possible.

One way is this :


#!/bin/bash
printf "Password please: "
stty -echo
read pass
stty echo
printf '\n'

Stay tuned.


giovedì 31 maggio 2012

Advanced Bash-Scripting Guide


Today I want to recommend an excellent manual on programming ofadvanced bash shell.


These are the links :


(html version) http://www.tldp.org/LDP/abs/html/
(pdf version) http://www.tldp.org/LDP/abs/abs-guide.pdf


Stay tuned

Bash shell script to see who is logged on or logged out every 3 seconds

Today a simple bash shell script to check every 3 seconds what user is logged in or logged out.
This script use "who" linux/unix command, result of these command is saved on a file that is checked every 3 seconds with new who execution :


#!/bin/bash
# + Giovanni Palleschi
# + 05/30/2012 13:23
# + Who is logged on or logged out every 3 seconds
echo 'The current time is:'
date
echo ' '
# List Current Users Connected
echo 'The current users are:'
who | cut -d' ' -f1 | sort -u > ./list_user_before
cat ./list_user_before
# Loop control login - logout every 3 seconds
while [ 1 ]; do
   sleep 3
# List Current Users Connected
   who | cut -d' ' -f1 | sort -u > ./list_user_after
# List of New Users Connected
   echo ' '
   diff ./list_user_before ./list_user_after | grep "^>" | cut -d' ' -f2 |
   while read new_user_login
   do
     echo 'User' $new_user_login 'has logged in.'
     echo ' '
   done
# List of Users Disconnected
   diff ./list_user_before ./list_user_after | grep "^<" | cut -d' ' -f2 |
   while read new_user_login
   do
     echo 'User' $new_user_login 'has logged out.'
     echo ' '
   done
   tot_users_diff=$(diff ./list_user_before ./list_user_after | wc -l)
   if [ $tot_users_diff -eq 0  ]; then
     echo 'No user has logged in/out in the last 3 seconds.'
   fi
# Move file list user after in file user before  for next control
   mv -f ./list_user_after ./list_user_before
done

Stay tuned.

giovedì 17 maggio 2012

Ksh script to remove shared memory

Today a simple script to remove shared memory in ksh :


#!/bin/ksh


#
# Shell Script to remove shared memory MVNO Instance
#


PID=$(ipcs -m | grep 0x000000a[0-9a-b] | awk '{print $2}'|xargs)
for one_pid in ${PID}; do
    ipcrm -m ${one_pid}
    echo 'Removed shm : ' ${one_pid}
done

This script remove all shared memory from address 0x000000a0 to 0x000000ab. You must modify this expression to remove another shared memory.

Stay tuned.


lunedì 5 marzo 2012

Arithmetic calculation on real numbers in Ksh Script

Today a simple program to execute arithmetic calculation with ksh script :

#!/bin/ksh
echo "Enter the Price of the item \$ \c"
read price
echo "What Percentage of discount is available \c"
read per
echo "Your Total Savings are \c"
sav=`echo "scale=3\n(($price * $per) / 100)" | bc -l`
echo $sav

Is important see this command :

sav=`echo "scale=3\n(($price * $per) / 100)" | bc -l`

With "scale=3" we are fixing number of decimals to print.
With "bc -l" we are calculating with unix command bc the discout applied, with
($price * $per) / 100.

Stay tuned.


Dice simulator

Today a simple shell script to simulate rolls of two dices :

#!/bin/ksh

fc=1

while [ fc -eq 1 ]; do

clear

echo ' How many rolls : \c'
read times

ind=1
while [ $ind -le $times ]; do

echo 'Roll Dice ' $ind
dice1=$RANDOM
dice2=$RANDOM

echo 'dice1 :' $(( dice1%6+1 ))
echo 'dice2 :' $(( dice2%6+1 ))

ind=$(( $ind + 1 ))
done

echo ' Continue(y/n) : \c'
read ans

if [ $ans = 'N' ] || [ $ans = 'n' ]; then
fc=0
fi

done


Stay Tuned.

mercoledì 29 febbraio 2012

Substring variable length record with awk.

The daily problem is this : I must print from third field to end of all records in a file where fields are separated from Tab character.

To resolve this problem i use awk in this way :

cat | awk -F" " '{ printf "%s\n",substr($0,index($0,$3)) }'

Where in -F awk option I specify tab character, and I use substr with index function of third field in each record read.

Stay tuned.





lunedì 18 luglio 2011

A good link for ksh shell scripting .....

Today i want share an interesting Ksh shell scripting link :


My problem is divide, in a shell script, file name from path.

Solutions was this :

file="/CLASS_09/CPP/INPUT/A8/pippo"
filename=${file##*/}
dirname=${file%/*}

echo $filename
echo $dirname

Stay tuned.



Check Mid Year Objectives

Hi all Today middle year check of my 2026's goals. 1. ENGLISH Improve listening comprehension (So and So)   See at least one or two film...