Commenting out "expr" creates weird behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting out "expr" creates weird behavior
# 1  
Old 11-11-2013
Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document
Code:
<<EOF
echo "Sum is: `expr $1 + $2`"
EOF

Even if I explicitly comment out the line containing the expr using "#", the error message would still exist
Code:
<<EOF
#echo "Sum is: `expr $1 + $2`"
EOF

And I observed that this happens only when I supply the 2nd operand to the expr as variable. If I hardcode it to a numeric value then the error message would disappear, that is
Code:
<<EOF
echo "Sum is: `expr $1 + 2`"   # expr's second argument numeric value is hard coded, no error now
EOF

Could anyone enlighten me on this?
# 2  
Old 11-11-2013
If you want to disable substitution in a heredoc then you need to quote the tag string:
Code:
<<'EOF'
echo "Sum is: `expr $1 + $2`"
EOF

This User Gave Thanks to CarloM For This Post:
# 3  
Old 11-11-2013
Quote:
Originally Posted by royalibrahim
Even if I explicitly comment out the line containing the expr using "#", the error message would still exist
A heredoc isn't a shell script. Since there are no comments within a heredoc, # is not a special character.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 11-11-2013
A here document is something that is just read (and expanded) and presented to stdin, and not executed per se. So the echo won't do anything. In the expansion phase, command substitution takes place, and expr will be executed. Are your positional parameters set? I guess not. Try set -- 3 5 and run again - does it succeed?
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Weird behavior of command "local"

Hi there, I'm running into a very weird situation. Let's forget about the purpose of my initial script please. I noticed the bug whatever I'm trying to do. I'm on an old server running bash 3.1.17. Say we have the following script : foo:~# cat /tmp/test #!/bin/bash f1() { local... (9 Replies)
Discussion started by: chebarbudo
9 Replies

2. Homework & Coursework Questions

How to lclear "expr: syntax error" eventhough everything looks fine?

Hi All, As per my knowledge in unix, my code looks fine. But still I am getting error (expr:syntax error). Please help me to resolve this error. Script : PRE_LBNO=0 PRE_DATE=0 TOT_PAY=0 TOT_REM=0 TOTAL=1 for Record_Type in `cut -c 1 Inputt.dat` do if ; then CURR_LBNO=` cut -c... (6 Replies)
Discussion started by: lathanandhini
6 Replies

3. UNIX for Advanced & Expert Users

Weird "sort" behavior

Hi, I'm trying to sort a text file "test": S12 S_S12 S_S1_12 S15 S_N15 S_N1_15 By "sort test", I get: S12 S15 S_N1_15 S_N15 S_S1_12 S_S12 It seems weird: Comparing Line 2 and Line 3, it must be that '-' is bigger than '1'; however, comparing Line 3 and Line 4, it seems that... (3 Replies)
Discussion started by: intermilan
3 Replies

4. UNIX for Advanced & Expert Users

"╭─ " Character combo in $PATH causes strange autocompletion behavior in zsh

I've posted about this before, but only recently narrowed the problem down to a specific cause. Ok, first of all, the behavior: It occurs when autocompletion brings up its list (not when there is only a single option). Basically, if I were to type, say, cd ~/<TAB> I would get something... (2 Replies)
Discussion started by: marshaul
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option? For example, if I have the example script (we'll name it ex.sh): #!/bin/sh if then echo "Bye." exit 2 fi And I execute it like this: >./ex.sh It... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

7. Shell Programming and Scripting

convert a=(b/100)*a into a "expr" expression

Hi Guys, THis is the first time am using the expr expression. I like to know how to write the expression a=(b\100)*a. THis works fine if it gives without a bracket. the bracket should be present as i wanted to define the order of execution. Help me out. Thanks for your help in advance.... (2 Replies)
Discussion started by: mac4rfree
2 Replies

8. Shell Programming and Scripting

HowTo: reg expr doing grep "timestamp>$DesiredTime" logfile ?

I know I asked a similar question but I want to know if there is a regular expression existing that with a korn shell cmd, finds any timestamp data records in a file where it is greater then a timestamp in a shell variable ? something like : grep all records where it has a timestamp >... (5 Replies)
Discussion started by: Browser_ice
5 Replies

9. Shell Programming and Scripting

why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit?

I found below script to check whether the variable is a digit in ksh. ############################ #!/bin/ksh REPLY="3f" if ]*\)'` != ${REPLY} && "${REPLY}" != "0" ]] then print "is digit\n" else print "not digit\n" fi ############################ Although it works fine, but... (6 Replies)
Discussion started by: sleepy_11
6 Replies

10. Shell Programming and Scripting

Weird problem with output from "date '+3600*%H+60*%M+%S' "

Hi, I came across a script a few months ago that allowed you to use the following script to include the current time into your prompt (useful from auditting purposes): # Set Prompt typeset -RZ2 _x1 _x2 _x3 let SECONDS=$(date '+3600*%H+60*%M+%S')... (5 Replies)
Discussion started by: m223464
5 Replies
Login or Register to Ask a Question