Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

unix - How to search using variable to match any string - shell

How to search part of the string using variable and assign to new variables

My Search variable is : db_uni_name=testdb_iac3bd

My Oratab File is:

+ASM1:/u01/app/12.2.0.1/grid:N
oidiaddb:/u02/app/oracle/product/12.2.0/dbhome_2:Y
testdb:/u02/app/oracle/product/12.2.0/dbhome_3:Y
oradb:/u02/app/oracle/product/12.2.0/dbhome_4:Y

I want to search $db_uni_name to find matching db name and path

In this case, i want to search for testdb and assign as follows:

DB_NAME=testdb
ORACLE_HOME=/u02/app/oracle/product/12.2.0/dbhome_3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

mayankp@mayank:~/Documents$ DB_NAME=$(echo $db_uni_name | grep `awk -F'_' '{print $1}'` Oratab.txt | awk -F ':' '{print $1}')
mayankp@mayank:~/Documents$ echo $DB_NAME
testdb
mayankp@mayank:~/Documents$ ORACLE_HOME=$(echo $db_uni_name | grep `awk -F'_' '{print $1}'` Oratab.txt | awk -F ':' '{print $2}')
mayankp@mayank:~/Documents$ echo $ORACLE_HOME 
/u02/app/oracle/product/12.2.0/dbhome_3

Let me know if this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...