LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Scripting .. (https://www.linuxquestions.org/questions/linux-general-1/shell-scripting-4175650170/)

Alok Behria 03-14-2019 09:46 AM

Shell Scripting ..
 
Hi All,

I have got the following code , which uses shell script to log on various database's running on server by setting up the database environment by getting the database SID in variable and passing this value further to set up the database SID and binary location.

Here on step 1 , I have got 1 confusion and not really sure what these commands does here.

#!/bin/ksh
ALL_DATABASES=`cat /etc/oratab|grep -v "^#"|grep -v "N$"|cut -f1 -d: -s` ## what does N$ and -s flag will do here and
for DB in $ALL_DATABASES
do
unset TWO_TASK
export ORACLE_SID=$DB
export ORACLE_HOME=`grep "^${DB}:" /etc/oratab|cut -d: -f2 -s`
export PATH=$ORACLE_HOME/bin:$PATH
echo "---> Database $ORACLE_SID, using home $ORACLE_HOME"
sqlplus -s "/as sysdba" <<-EOF
select * from global_name;
exit;
EOF
done

Regards

rtmistler 03-14-2019 10:17 AM

Please use [code] tags when posting code. It helps the readability.

N$ is purely a string that the grep is searching for.

The -s flag is part of the cut command. Please check the manual page for cut, which explains that it will not print lines that do not contain a delimiter. Note: The -d flag defines what said delimiter is to be.

TB0ne 03-14-2019 10:19 AM

Quote:

Originally Posted by Alok Behria (Post 5973803)
Hi All,
I have got the following code , which uses shell script to log on various database's running on server by setting up the database environment by getting the database SID in variable and passing this value further to set up the database SID and binary location. Here on step 1 , I have got 1 confusion and not really sure what these commands does here.
Code:

#!/bin/ksh
ALL_DATABASES=`cat /etc/oratab|grep -v "^#"|grep -v "N$"|cut -f1 -d: -s`  ## what does N$ and -s flag will do here and
for DB in $ALL_DATABASES
do
  unset  TWO_TASK
  export ORACLE_SID=$DB
  export ORACLE_HOME=`grep "^${DB}:" /etc/oratab|cut -d: -f2 -s`
  export PATH=$ORACLE_HOME/bin:$PATH
  echo "---> Database $ORACLE_SID, using home $ORACLE_HOME"
  sqlplus -s "/as sysdba" <<-EOF
select * from global_name;
exit;
EOF
done


You have been asking about shell scripts for SEVEN YEARS now; you also need to read the "Question Guidelines" link in my posting signature. We are happy to help you, but you have to show effort of your own and do basic research. Have you actually tried to look at the command? Read the man pages for the various pieces, and the options that they use? Or even just tried to run that command-chain in a terminal, and change things to see the results??

If you want to know what it does, then you need to read the man pages for the grep and cut commands.

berndbausch 03-14-2019 06:08 PM

Quote:

Originally Posted by rtmistler (Post 5973814)
Please use [code] tags when posting code. It helps the readability.

N$ is purely a string that the grep is searching for.

The -s flag is part of the cut command. Please check the manual page for cut, which explains that it will not print lines that do not contain a delimiter. Note: The -d flag defines what said delimiter is to be.

Also, the cat /etc/oratab is unnecessary and is known as “cat abuse”. Add /etc/oratab to the grep command.

pan64 03-24-2019 05:53 AM

the simple way is to try:
Code:

cat /etc/oratab|grep -v "^#"|grep -v "N$"|cut -f1 -d: -s # with and/or without -s
cat /etc/oratab|grep -v "^#"|cut -f1 -d: -s              # with and/or without -s
# anything else you want

but obviously you can read man cut to check -s. and man grep

tyler2016 03-24-2019 08:20 AM

grep -v "N$" will give you all lines that do NOT end in N. $ in regular expressions means the end of the line. I think (could be wrong) in /etc/oratab a line that ends in N means that SID set to not start at boot.

From the cut man page:

Code:

      -s, --only-delimited
              do not print lines not containing delimiters



All times are GMT -5. The time now is 05:39 AM.