Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Monday, August 23, 2021

Shell Scripting

 Shell Scripting Handy

User creation at OS level (usercreate_os.sh)

#!/bin/bash

#this script creates an account on the local system.

#you will be prompter for the account name and password

#Ask for username

read -p 'Enter the username: ' USER_NAME

#Ask for the real name

read -p 'Enter the name of the person who is this account for: ' COMMENT

#ask for the password

read -p 'Enter the password to use for the account: ' PASSWORD

#create the username

useradd -c "${COMMENT}" -m ${USER_NAME}

#set the password for the username

echo ${PASSWORD} | passwd --stdin ${USER_NAME}

#force password change on first login

passwd -e ${USER_NAME}

######################################################################

Script2:

 RANDOM Each time this parameter is referenced, a random integer between 0 and 32767

              is  generated.  The sequence of random numbers may be initialized by assign‐

              ing a value to RANDOM.  If RANDOM is unset, it loses its special properties,

              even if it is subsequently reset.


[oracle@kolkata02 ~]$ echo ${RANDOM}

24092

[oracle@kolkata02 ~]$ echo ${RANDOM}

1748

[oracle@kolkata02 ~]$ echo ${RANDOM}

2398

[oracle@kolkata02 ~]$ !v   ----> this will opens the last closed file

#!/bin/bash

#This scripts generates a list of random passwords

#A random number as a password

PASSWORD=${RANDOM}

echo "${PASSWORD}"

#Three random numbers together

PASSWORD="${RANDOM}${RANDOM}${RANDOM}"

echo "${PASSWORD}"

#use the current date/time as the basis for the password

PASSWORD=$(date +%s)    ---->'+' is for format and %s is for seconds since 1970 UTC

echo "${PASSWORD}"

#use nano seconds to act as randomization

PASSWORD=$(date +%s%N)   --->%N is the nano seconds

echo "${PASSWORD}"

# A better password

PASSWORD=$(date +s%N | sha256sum | head -c32)

echo "${PASSWORD}"

# An even better passsword

PASSWORD=$(date +s%N${RANDOME}${RANDOM} | sha256sum | head -c32)

echo "${PASSWORD}"

# An even better passsword

Special_character=$(echo '!@#$%^&*()_+' | fold -w1 | shuf | head -c1)

echo "${PASSWORD}${Special_character}"  --->here special character will be appended

++++++++++++++++++++++++
[oracle@kolkata02 ~]$ echo "1" >> cheksumdata.txt   ---->this will append the data in the next line
[oracle@kolkata02 ~]$ vi cheksumdata.txt
[oracle@kolkata02 ~]$ echo "2" >> cheksumdata.txt       ---->this will append the data in the next line
[oracle@kolkata02 ~]$ vi cheksumdata.txt
asdfdsdfasdf34343434
1
2
++++++++++++++++++++++++
head -2 /etc/passwd
head -n1 /etc/passwd
head -n -1 /etc/passwd
head -c1 /etc/passwd
head -c2 /etc/passwd
echo "testing" | head -c2
date +%s%N | sha256sum | head -c32
++++++++++++++++++++++++
Parameter is the variable using inside the shell script
Argument is the value passing to the parameter
${0} ---> is the positional parameter, which takes filename itself
[oracle@kolkata02 ~]$ which head        ---->which -a head
/usr/bin/head
${#} ---> it tells number of arguments you passed to the script
${@} ---> this will be used in for loop and we dont know how many user input will be passed as arguments
${*} ---> This is consider/combine all the user inputs/arguments into a single argument
[oracle@kolkata02 ~]$ for username in zameer naseer ayaan
> do 
> echo hi ${username}
> done
hi zameer
hi naseer
hi ayaan
[oracle@kolkata02 ~]$

#!/bin/bash
echo 'you executed this command: '${0}

true
sleep
shift
while loop
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#This script demonstrates I/O redirection
#standard input
#standard output
#standard error
#redirect STDOUT to a file
file="/tmp/data"
head -n1 /etc/passwd > ${file}

#redirect STDIN to a program
read LINE < ${file}
echo "the line contains : ${file}"

#we can change the password of the user by redirecting the output from password file to passwd
[oracle@kolkata02 ~]$ echo "secret" > password
[oracle@kolkata02 ~]$ cat password
secret
[root@kolkata02 oracle]# sudo passwd --stdin testuser1 < password
Changing password for user testuser1.
passwd: all authentication tokens updated successfully.
# ">" overwrite the existing content in a file
head -n3 /etc/passwd > ${file}
echo contents of the file ${file} is:
cat ${file}

#redirect STDOUT to a file, appending to the file
echo "${RANDOM} ${RANDOM}" >> ${file}
echo "${RANDOM} ${RANDOM}" >> ${file}
echo
echo "contents of the file: ${file}"
cat ${file}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[oracle@kolkata02 ~]$ read x < /etc/redhat-release
[oracle@kolkata02 ~]$ echo ${x}
Red Hat Enterprise Linux Server release 7.9 (Maipo)
[oracle@kolkata02 ~]$ read x 0< /etc/redhat-release
[oracle@kolkata02 ~]$ echo ${x}
Red Hat Enterprise Linux Server release 7.9 (Maipo)
[oracle@kolkata02 ~]$
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out    --->this will not redirect the error to the file head.out
head: cannot open ‘/fakefile’ for reading: No such file or directory
[oracle@kolkata02 ~]$ cat head.out
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash

==> /etc/hosts <==
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
[oracle@kolkata02 ~]$
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out 2>head.err  ---> this will write the error into the file head.err
[oracle@kolkata02 ~]$ cat head.err
head: cannot open ‘/fakefile’ for reading: No such file or directory
the below 2>>head.err will append the error
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out 2>>head.err
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out 2>>head.err
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out 2>>head.err
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.out 2>>head.err
[oracle@kolkata02 ~]$ cat head.err
head: cannot open ‘/fakefile’ for reading: No such file or directory
head: cannot open ‘/fakefile’ for reading: No such file or directory
head: cannot open ‘/fakefile’ for reading: No such file or directory
head: cannot open ‘/fakefile’ for reading: No such file or directory
head: cannot open ‘/fakefile’ for reading: No such file or directory
[oracle@kolkata02 ~]$
What if we wanted to send the standard output and standard error to the same file, we will see the old syntax and new syntax for that
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile > head.both 2>&1
[oracle@kolkata02 ~]$ cat head.both
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash

==> /etc/hosts <==
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
head: cannot open ‘/fakefile’ for reading: No such file or directory
[oracle@kolkata02 ~]$
The new syntax for the same above operation is as below
[oracle@kolkata02 ~]$ head -n1 /etc/passwd /etc/hosts /fakefile &> head.both
[oracle@kolkata02 ~]$ cat head.both
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash

==> /etc/hosts <==
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
head: cannot open ‘/fakefile’ for reading: No such file or directory
[oracle@kolkata02 ~]$
&>> ---> will append the output

===============Below is to display the abended process logs================

#!/bin/bash
BASE_DIR=/u01/app/oracle/scripts
log_dir=$BASE_DIR/log_dir
gg_processes=$BASE_DIR/log_dir/gg_processes.log
gg_process_Detail=$BASE_DIR/gg_process_Detail.txt
Abended_ggprocess=$BASE_DIR/Abended_ggprocess.txt
gg_process_log_output=$BASE_DIR/gg_process_log_output.txt
rm ${gg_process_log_output}
echo $ORACLE_HOME
echo $GG_HOME
cd $GG_HOME
pwd

$GG_HOME/ggsci <<EOF > ${gg_processes}
info all
exit
EOF

cat ${gg_processes} | grep ABENDED | awk '{ print $3 }' > ${Abended_ggprocess}

while read line
do
#echo -e "\n"
#echo -e "The below is the output of last four lines of ${line} GG Process log, please check\n" >>${gg_process_log_output}
echo -e "The below is the output of last four lines of `echo -e "\e[1;31m ${line} \e[0m"`  GG Process log, please check\n" >>${gg_process_log_output}
tail -4 /u01/app/oracle/product/ogg/dirrpt/${line}.rpt >> ${gg_process_log_output}
echo -e "\n" >> ${gg_process_log_output}
done <${BASE_DIR}/Abended_ggprocess.txt

========================================================


FIG project queries

##### Service add & LOad Baclancing on Add Service ####### srvctl add service -s wcccdmt.farmersinsurance.com -r wcccdmtx1,wcccdmtx2,wcc...