syntax error in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntax error in shell script
# 1  
Old 07-31-2012
syntax error in shell script

I am creating a shell script. In which, I need to get server name and server IP. I used this command in script.
Code:
servername=`cat /etc/hosts|grep `eval hostname`|awk '{print $2}'`

however, when execute script or put set -x to debug, it return:
line 13: syntax error at line 13: `|' unexpected

I typed this line of command at command prompt, it works with exact server name returned. Please help me to identify where is the error. Thanks.

Last edited by duke0001; 07-31-2012 at 01:50 PM.. Reason: code tags
# 2  
Old 07-31-2012
Hi,

Try;

Code:
servername=`uname -n`

Regards

Dave
# 3  
Old 07-31-2012
You have doubly nested backtics - some shells have a problem with that try:
Code:
servername=$( cat /etc/hosts|grep `eval hostname`|awk '{print $2}' )

Also consider using nslookup, since not all server names will be in /etc/hosts
# 4  
Old 07-31-2012
gull04, jim:

Thanks so much for your prompt advise. I tested two commands. They are all work very well. I will use them. I chnaged my shell from
#!/bin/ksh to #!/bin/bash

My question remaining is, after I check and chnage my shell in script. It is Oracle Linux 5. So shell is bash shell. I used original script to test. it return this error code:

./script.sh: command substitution: line 13: syntax error near unexpected token `|'

It is pointed to the same error as begining. Why shell flavor has big difference?
# 5  
Old 07-31-2012
The big flavor difference is: you are depending on what is called undefined behavior.
POSIX standards no not say what happens with nested backtics, for example. So some nice guy who wrote shellA allows it to work correctly (correctly == your expectations, not really required to work). The evil guy who wrote shellB did not do that. He literally follows the standard

from opengroup.org (POSIX people):
Quote:
2.6.3 Command Substitution
Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution shall occur when the command is enclosed as follows:

$(command)


or (backquoted version):

`command`


The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment ) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline> characters at the end of the substitution. Embedded <newline> characters before the end of the output shall not be removed; however, they may be treated as field delimiters and eliminated during field splitting, depending on the value of IFS and quoting that is in effect. If the output contains any null bytes, the behavior is unspecified.

Within the backquoted style of command substitution, <backslash> shall retain its literal meaning, except when followed by: '$' , '`' , or <backslash>. The search for the matching backquote shall be satisfied by the first unquoted non-escaped backquote; during this search, if a non-escaped backquote is encountered within a shell comment, a here-document, an embedded command substitution of the $( command) form, or a quoted string, undefined results occur. A single-quoted or double-quoted string that begins, but does not end, within the "`...`" sequence produces undefined results.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax error C shell

Hello, I have a newbe syntax error but I cant find it syntax error: unexpected end of file #!/bin/csh # pe request #$ -pe mpi_16 32 #### 16 core : 'mpi_16 16' || 24 core : 'mpi_24 24 ' # our Job name #$ -N test2MD #$ -S /bin/sh (1 Reply)
Discussion started by: dulceC
1 Replies

2. Shell Programming and Scripting

Shell scripting syntax error in if then else

I have one bash shell script to execute table refreshment. At the bottom of script, I have one piece of code to check 'ORA-' error from log file, then send email to DBA or application people. But this piece of code didn't work. I tried different ways and also search online to find where is my... (2 Replies)
Discussion started by: duke0001
2 Replies

3. Shell Programming and Scripting

SHELL: syntax error near unexpected token `else'

Hi, I wrote this shell script to be a tool for my team to do daily routine works. Though, option a and b are working fine, option c is giving below error. line 647: syntax error near unexpected token `else' line 647: `else' Here s my script. A bit lengthy though. Pls excuse. Also,... (3 Replies)
Discussion started by: Naga06
3 Replies

4. HP-UX

HP-UX: Shell Script giving " 0^J30: Syntax error"

Hi All, We are getting a very unique error while running a shell script on HP-UX box. Can somebody help in this regards? The shell script is working fine on linux/solaris box. Error: ++++++++++++++++++++++++ $/test.sh ./test.sh: 0^J30: Syntax error $ ++++++++++++++++++++++++ TIA.... (16 Replies)
Discussion started by: vai_sh
16 Replies

5. Shell Programming and Scripting

Compare two initoras shell script syntax error

Hi All, I am executing the below shell script to compare two initoras files which is ending up in syntax error as below: -bash-3.2$ cat compare_two_initoras.sh #!/bin/bash file1=$1 file2=$2 for parameter in `cat $file1 | grep = | awk '{print $1}' | grep -v '#'` do value1=`grep... (4 Replies)
Discussion started by: a1_win
4 Replies

6. Shell Programming and Scripting

Syntax Error in Unix Shell Script

I am trying to run a unix script in my home directory.Snippet below echo "`date '+%Y%m%d_%H%M%S'` Getting ProductList.dat" if ( -f $DIR/ProductList.dat) then cp $DIR/ProductList.dat MigratedProductList.dat else echo "`date '+%Y%m%d_%H%M%S'`ProductList.dat does not exist; Processing... (4 Replies)
Discussion started by: Mary James
4 Replies

7. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

8. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies

9. UNIX for Dummies Questions & Answers

Shell Syntax Error when copying files

Hello, I apologize if this was addressed in a previous post. I have done some searching but may have missed it. I am trying to read a list from a file, for example: 3bik 3bix 3biu 3bin 1nwn and using this list, copy files with these names (ex: 3bik.dssp.Z) to a seperate folder,... (1 Reply)
Discussion started by: InfoSeeker
1 Replies

10. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question