Thursday, September 7, 2017

Batch script to check if file exist or not


This is a simple windows batch script to check if a file exists or  not



ECHO CHECKING IF FILE EXISTS
set FILEEXISTS=FILE_NAME                          REM ENTER FILENAME HERE
if exist %FILEEXISTS% (DO SOMETHING)
ELSE (DO SOEMTHING ELSE )

Thursday, December 8, 2016

handling blank iputs in a shell script.


Here is simple script to handle blank inputs in your interactive shell scripts.

#!bin/sh
while :
echo "enter value for a:";read -r a;  do
         if [ -z "${a}" ]; then
         echo "That was empty, do it again!"

        else ## echo "Checking now..."
                echo "value passed is :$a"
                echo "valid input received."
                break
    fi
done

echo "We are out of loop:"

Sample Output :

[Linux01] $ . Test_input11.sh
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:
1
value passed is :1
valid input received.
We are out of loop:

Error handling in shell script


Error handling in shell script while receiving user input

 
This is help full sample script that can help in error handling.In situations where I give user to choose 1 or 2 but lets say in rush/hurry user inputs a diff value. This script can handle those situations. In below script the user will be in loop until he passes a expected value (1 or 2 in this case). This script can be altered as per your requirement.

On how to handle just blank inputs use this link


-- Script starts here

#!bin/sh
## Created by     :    Arvind Toorpu
## This script will prompt user for input until the user passes the expected input.
##
while :
echo "enter value for a:";read -r a;  do
         if [ -z "${a}" ]; then
         echo "That was empty, do it again!"

        elif [ "${a}" -eq "1" ]  || [ "${a}" -eq "2" ]; then
                ## echo "Checking now..."
                echo "value passed is :$a"
                echo "Condition success"
         break

        elif [ "${a}" -ge "3" ]
        then
        echo "Select either option :1 or :2 "
    fi
##break
done


Sample output :

atoorpu@Linux01:[~/TEST_DIR/scripts] $ . Test_input2.sh
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:
4
Select either option :1 or :2
enter value for a:
5
Select either option :1 or :2
enter value for a:
6
Select either option :1 or :2
enter value for a:
6
Select either option :1 or :2
enter value for a:
7
Select either option :1 or :2
enter value for a:
2
value passed is :2
Condition success
do your thing here with read value 2

Monday, September 26, 2016

service enable slapd enable: unrecognized service


Problem: 

[root@linuxOS cn=config]# service start slapd
start: unrecognized service


[root@linuxOS cn=config]# start slapd
start: Unknown job: slapd


[root@linuxOS cn=config]# start slapd
start: Unknown job: slapd


Solution:
 Use the start command after ServiceName

Syntax :
# service ServiceName Start

[root@linuxOS app]# service slapd start

Checking configuration files for slapd:                    [WARNING]
57e427f1 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
57e427f1 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={2}bdb.ldif"
config file testing succeeded
Starting slapd:                                            [  OK  ]

Friday, September 2, 2016

Print two file in NIX machines

 

To print two files side by side we can use pr command. This can be used on Unix\Linux provided that package is installed.

Print two files side by side :


atoorpu@Linux01:[~/tmp] $ pr -m -t t1.txt t2.txt
abcd                                abcd2
efgh                                efgh
ijkl                                  ijkl12
mnop                              mnop


 ADDING SOME SPACE TO T1 IT LOOKS LIKE THIS:

atoorpu@Linux01:[~/tmp] $ pr -m -t t1.txt t2.txt
AB CD                               abcd2
EF GH                               efgh
ij kl
mnop                                ijkl12
                                    mnop
                                   
reference for pr cmd     : http://linux.about.com/library/cmd/blcmdl1_pr.htm

compare two files in Linux\Unix

 

We can use the diff cmd to compare files on Linux\Unix machines.



atoorpu@Linux01:[~/tmp] $ cat t1.txt
abcd
efgh
ijkl
mnop

atoorpu@Linux01:[~/tmp] $ cat t2.txt
abcd2
efgh
ijkl12
mnop


atoorpu@Linux01:[~/tmp] $ diff -y t1.txt t2.txt
abcd                                                          | abcd2
efgh                                                            efgh
ijkl                                                          | ijkl12
mnop                                                            mnop


[option] -y     :    Use the side by side output format.


you can also set width to print columns using the -W, --width=NUM option:

atoorpu@Linux01:[~/tmp] $ diff -y -W 20 t1.txt t2.txt
abcd  | abcd2
efgh    efgh
ijkl  | ijkl1
mnop    mnop

To ignore the case sensitivity between two files use -i :

atoorpu@Linux01:[~/tmp] $ diff -y -i t1.txt t2.txt
ABCD                                                          | abcd2
EFGH                                                            efgh
ijkl                                                          |
                                                              > ijkl12
mnop                                                            mnop

To ignore white spaces between two files use --ignore-all-space :

ADDING SOME SPACE TO T1 IT LOOKS LIKE THIS:
atoorpu@Linux01:[~/tmp] $ pr -m -t t1.txt t2.txt
AB CD                               abcd2
EF GH                               efgh
ij kl
mnop                                ijkl12
                                    mnop



To compare two directories you can use :

atoorpu@Linux01:[~/tmp] $ diff -y /home/atoorpu/tmp/A /home/atoorpu/tmp/B
diff -y /home/atoorpu/tmp/A/t1.txt /home/atoorpu/tmp/B/t1.txt
AB CD                                                           AB CD
EF GH                                                           EF GH
ij kl                                                           ij kl
mnop                                                            mnop
Only in /home/atoorpu/tmp/A: t2.txt


reference for diff cmd     :  http://linux.about.com/library/cmd/blcmdl1_diff.htm

gpg encryption helpfull cmds and examples


to create a key:
gpg --gen-key
generally you can select the defaults.

to export a public key into file public.key:
gpg --export -a "User Name" > public.key
This will create a file called public.key with the ascii representation of the public key for User Name. This is a variation on:
gpg --export
which by itself is basically going to print out a bunch of crap to your screen. I recommend against doing this.
gpg --export -a "User Name"
prints out the public key for User Name to the command line, which is only semi-useful

to export a private key:
gpg --export-secret-key -a "User Name" > private.key
This will create a file called private.key with the ascii representation of the private key for User Name.
It's pretty much like exporting a public key, but you have to override some default protections. There's a note (*) at the bottom explaining why you may want to do this.

to import a public key:
gpg --import public.key
This adds the public key in the file "public.key" to your public key ring.

to import a private key:
NOTE: I've been informed that the manpage indicates that "this is an obsolete option and is not used anywhere." So this may no longer work.
gpg --allow-secret-key-import --import private.key
This adds the private key in the file "private.key" to your private key ring. There's a note (*) at the bottom explaining why you may want to do this.

to delete a public key (from your public key ring):
gpg --delete-key "User Name"
This removes the public key from your public key ring.
NOTE! If there is a private key on your private key ring associated with this public key, you will get an error! You must delete your private key for this key pair from your private key ring first.

to delete an private key (a key on your private key ring):
gpg --delete-secret-key "User Name"
This deletes the secret key from your secret key ring.

To list the keys in your public key ring:
gpg --list-keys

To list the keys in your secret key ring:

gpg --list-secret-keys