Shashikant shah

Wednesday, 31 December 2014

How to set Variable in script

#!/bin/bash

source /root/shashi/credentials.prop

env=$1

if [ "$env" = = '' ] || [ "$username" = = '' ] || [ "$password" = = '' ];then

        echo "usuage ./build.sh"
exit
fi

source /root/shashi/properties

if [[ $codebasedir == '' ]]; then

        echo -n "Please provide tpd all package file:"
        read -s codebasedir
        echo
fi

[ ! -d "log" ] && mkdir log
[ ! -d "$codebasedir" ] && echo code base dir not found.

##validation for curl installation

verify_curl()
{
which curl > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "curl is not installed"
exit
else
echo "curl version is : `curl --version | head -1 |cut -d" " -f1,2`"
fi
}

##first test that we have the correct password
verify_password()
{
for x in `cat /root/shashi/environment/"$env"`
do
curl -s -f -k --user $username:$password $x/home/users/a/$username.json | grep -q 'rep'
if [ $? -eq 0 ]
then
echo "Username and Password is correct :$x"
else
echo "you don't have the correct username or password : $x"
exit
fi
done
}
check_app(){
if [ ${env} = = 'true' ] ; then 
        verify_curl
        verify_password
fi     
}
verify_curl
verify_password
check_app

===============================================================
#!/bin/bash
jail="${1:-/home/shashi}"
echo "set at ${jail}"

#!/bin/bash
var=${USER:-not_found}

=====================
#!/bin/bash
var=${USER:=value}


#!/bin/bash
var=${USER:value}
d=${var:=not_found}

sh -x script.sh
====================

#!/bin/bash
path=${1:?ERROR command line arg not passed}
echo "Backup path is $path."
echo "I m done if \$path is set."

./script.sh /home

Friday, 5 December 2014

How to replace a string in a file

#!/bin/bash

echo -n "Enter word:- "
read word

echo -n "location Search:- "
read search

E=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq`
I=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq | wc -l`

echo "$E" > /test/log

if [ "$I" == 0 ];then

        echo "file is not found."
else

echo -n "Enter Replace word:- "

read new

for x in `cat /test/log`
   do
        sed -i "s/$word/$new/g" $x
done
echo "***** Repalce file name logs '/test/log' ******"
echo "*****.Replace has been done.*****"

fi