Escape and command substitution in scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escape and command substitution in scripting
# 1  
Old 10-25-2017
Escape and command substitution in scripting

I have one question in shell script for escape "\" with command substitution "` `". I post there to seek help to understand how it works.

My original one piece of code in script like this: This piece of code in whole script is working without errors
Code:
chk_mode=`sqlplus -s /nolog<<EOF
connect / as sysdba
set pagesize 0 feedback off heading off
select open_mode from v\\\$database;
EOF`

Usually if I want to escape $ sign from v$database, I should use "v\$database". Why is this piece of code using 3 backslash as v\\\$database? Is this because the backslash is in command substitution(backtick)? I tested in command line: only v\$database works, v\\\$database is not working. My Unix server is solaris 11.3 and SHELL is bash.

Please help me to understand why using 3 backslashes as escape. Thanks for your advice.

Moderator's Comments:
Mod Comment You have been asked to use code tags repeatedly over the years. This is your last warning. The next time you post without using code tags you will be banned from the site.
# 2  
Old 10-26-2017
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
These 3 Users Gave Thanks to Neo For This Post:
# 3  
Old 10-26-2017
Yes it is because the command is backticks. This is one of the reasons to avoid using this deprecated form of command substitution. The preferred command substitution method uses $( ... ).

Try this instead:
Code:
chk_mode=$(sqlplus -s /nolog<<EOF
connect / as sysdba
set pagesize 0 feedback off heading off
select open_mode from v\$database;
EOF
)

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-26-2017
Did you consider - if no other expansion needs to be done within the here document - to switch off expansion therein (from man bash):
Quote:
Here Documents
. . .
If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.
?
# 5  
Old 10-26-2017
appreciation

Scrutinizer:

Thank you so much for your advice. You helped me to understand this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regarding command substitution

Oracle Linux 5.6, 64-bit Given the following snippet wrkvar=`sqlplus -s / as sysdba <<EOF set echo off feedback off head off trimsp on select count(*) from v\$parameter where name in ('db_file_name_convert','log_file_name_convert') and value is not null; EOF` echo wrkvar=$wrkvarProduces... (2 Replies)
Discussion started by: edstevens
2 Replies

2. Shell Programming and Scripting

Auto escape script to escape special chars in script args

This is a bit off the wall, but I often need to run scripts where there are argument values that contain special characters. For example, $ ./process.exe -t M -N -o temp.mol.s -i ../molfiles/N,N\',N\'\'-trimethylbis\(hexamethylene\)triamine.mol && sfile_space_to_tab.sh temp.mol.s temp.s It... (1 Reply)
Discussion started by: LMHmedchem
1 Replies

3. Shell Programming and Scripting

Using C-Shell Scripting for Substitution

mv myFile.txt myFile.txt.bak sed s/foo/bar/g myFile.txt.bak > myFile.txt Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters: the string to be replaced the string with which to replace it the name of the file in which to make the... (1 Reply)
Discussion started by: RogerW
1 Replies

4. Shell Programming and Scripting

Shell scripting substitution techniques

Hi guys, I'm looking for some thoughts on this. I'm trying to do a clean 1 liner to substitute some values. What I have: sed 's/Personid=.*/Personid=xxxxxx/' $tmpFileOut sed 's/Person:.*/Person:xxxxxx/' $tmpFileOut sed 's/PersonID:.*/PersonID: xxxxxx/' $tmpFileOut Obviously that's a bit... (1 Reply)
Discussion started by: rich@ardz
1 Replies

5. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

6. Shell Programming and Scripting

Using awk with the date command and escape characters

I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time. I am using the date command in conjunction with awk to try to do this. Just myfile: 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813 28/Aug/1995:00:00:38 1... (3 Replies)
Discussion started by: jontjioe
3 Replies

7. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

8. Shell Programming and Scripting

Escape Space in a ssh command

The following statement does work. But the second command does not work as expected. ssh root@123.123.123.123 \\"mysqldump -h localhost -u root -pPassWord dbName -d | gzip -cf\\" | gunzip -c > database1.sql ssh root@123.123.123.123 \\"mysqlbinlog /var/log/mysql/mysql-bin.*... (0 Replies)
Discussion started by: shantanuo
0 Replies

9. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

10. Shell Programming and Scripting

Substitution of last command

"Is there any substituation of last command or script syntax which can be used as a user. As far I know the "last" command is being used to display information about previous logins. A member of adm group or the user adm can execute it only. Thanks in advance for your usual help. Ghazi (6 Replies)
Discussion started by: ghazi
6 Replies
Login or Register to Ask a Question