Thursday, 31 October 2013

How to find the last argument passed to a Shell Script:-

To find the last argument passed to a Shell Script:

$1 - first arguments.

$* / $@ -  all arguments.

$# - number of arguments.

Here is a script to find the last argument passed,

# cat arguments.sh
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No Arguments supplied"
else
echo $* > .ags
sed -e 's/ /\n/g' .ags | tac | head -n1 > .ga
echo "Last Argument is: `cat .ga`"
fi

Output:

# ./arguments.sh
No Arguments supplied

# ./arguments.sh testing for the last argument value
Last Argument is: value

Saturday, 12 October 2013

Run commands periodically without cron is possible?

In Linux - can run periodical commands without cron ??!??.......

Yeah, Running commands periodically without cron is possible when we go with "while".

As a command:

# while true; do <your command here> ; sleep 100; done &

Example: # while true; do echo "Hello World" ; sleep 100; done &

do not forget the last "&" as it will put your loop in the background.

Same way to call a script,

crate file name: while_check.sh

# cat while_check.sh
#!/bin/bash
while true; do /bin/sh script.sh ; sleep 100; done &

# cat script.sh
echo "Hello World"

# ./while_check.sh

Is it useful??

Random Manual(command's) Pages while login SSH:

  • If you wish to know the linux command's random man page in every SSH login, Kindly add below line in .bashrc file,

/usr/bin/man $(ls /bin | shuf | head -1)

  • Now you got it, useful right???

find difference between two files using Shell Script - BASH

Use below script to find difference between two file in faster way.....

1). copy paste below script in simplediff.sh

2). chmod 755 simplediff.sh

=====
#!/bin/bash
echo -e "Enter the full path for FILE1:"
read f
echo -e "Enter the full path for FILE2:"
read g
if [ ! -f $f ] || [ ! -f $g ]
then
echo "FILE1 or FILE2 MISSING"
else
echo -e "Enter ( 1 ) to konw Different contents in FILE1\nEnter ( 2 ) to know the Different contents in FILE2"
read h
case "$h" in
1) clear
echo -e "File1:"
echo -e "(First line shows the file's Last Modify time)\n"
/usr/bin/diff -u $f $g | grep '^-'
if [ $? == 1 ]
then
clear
echo -e "\033[33;32m"
echo -e "\n\nNo Different contents Found !\n\n"
echo -e "\033[33;0m"
fi
;;
2) clear
echo -e "File2:"
echo -e "(First line shows the file's Last Modify time)\n"
/usr/bin/diff -u $f $g | grep '^+'
if [ $? == 1 ]
then
clear
echo -e "\033[33;32m"
echo -e "\n\nNo Different contents Found !\n\n"
echo -e "\033[33;0m"
fi
;;
*) clear
echo -e "\033[33;31m"
echo -e "\nWrong Option selected !!!\n"
echo -e "\033[33;0m"
;;
esac
fi

=====



If there is no difference in the mentioned files, you will get



If there is no files or wrong path you have mentioned,




When wrong choice made !!


Otherwise you will get result .........