Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell script runing error when using variable

Posted on 2012-03-29
9
Medium Priority
?
589 Views
Last Modified: 2012-08-13
Hello Experts,

I have a problem with my shell script. When I used no variable in command line, script can be run correctly. But when I used variable  $LLF instead of "file name" I have got error. Please see detail below

=========================
Main problem
=========================
command:
cat reqid_107.txt | awk '{print "cat $LLF | grep DEBUG | grep "$1" | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1" }' | sh

error:
cat: cannot stat -

=========================
When I used this command below (same command without "| sh"), I got result as (Defined $LLF=<file name>
=========================
command:
cat reqid_107.txt | awk '{print "cat $LLF | grep DEBUG | grep "$1" | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1" }'

result:
cat $LLF | grep DEBUG | grep 018CD56FE539469886571EB2BED7CE2E | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 01FF3697FC114DFCA4CBE8C1430BFE6D | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 16BA9D1313BB4E07ACB9F5C76B46CE29 | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 2EFED5D4F2D54A5C9CCDD58B11167DFE | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 440281729BB44C09B3BA016A0F722CAE | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 5087540EA4F445B1961E9DB68BBDA5F0 | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 641A2973567D411D9002F7343321995B | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 6CDB9385D72E4E9E90CCF0CE8598B24B | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 7525331A39FE4101878B7495C437186A | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep 80EEF1D4CBB44FD1A5AE8AA989C12703 | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep B3852EA932C04DB9BF6B44F8DBF24AAE | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep C74D8965D4084719836C303D7E1DD34A | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep CF140824D2FB4B2EB30412E21709A6AB | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep CFA0F695F95B4E82A536B9C033E984B5 | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep E121E98858DB4F6796CF0C5E112E98AA | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1
cat $LLF | grep DEBUG | grep EBC06D59C540414F83816D311B6AD8B4 | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1

=========================
That each line above can be run correctly.
Thank in advance.
=========================
0
Comment
Question by:trazodone
9 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 37780886
>>cat: cannot stat -

is the only error u get?
0
 
LVL 10

Expert Comment

by:Pierre François
ID: 37780907
You are mixing up the syntax of some high-level languages (java, php, ...) and the shell for defining a variable:

Most languages:
$LLF="somestring";

Open in new window


Shell script:
LLF="some string"

Open in new window

without $ at the beginning of the line.
0
 

Author Comment

by:trazodone
ID: 37780982
yes only this error
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:trazodone
ID: 37780990
I use below:
LLF=`ls -lrt mt-mm-mtnu.log.* | awk '{t=$9}END{print t}'`

that got result as file name below:
mt-mm-mtnu.log.2012-03-28

***LLF is result from running following command:
ls -lrt mt-mm-mtnu.log.* | awk '{t=$9}END{print t}
0
 

Author Comment

by:trazodone
ID: 37781012
If I replace:
cat reqid_107.txt | awk '{print "cat $LLF | grep DEBUG | grep "$1" | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1" }' | sh

With file name:
cat reqid_107.txt | awk '{print "cat mt-mm-mtnu.log.2012-03-28 | grep DEBUG | grep "$1" | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1" }' | sh

*** Script working correctly
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37781064
Let's go back a few steps and get you to explain the data and the result you want.

The construct you are trying to use is almost certainly not the best approach.
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 37781074
Did you want
cat reqid_107.txt | awk '{print "cat '$LLF' | grep DEBUG | grep "$1" | grep msisdn | cut -c 353-362 | sort | uniq >> err107.1" }' | sh
?
0
 

Author Closing Comment

by:trazodone
ID: 37781363
Thank you very much :)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37784330
You really would be better off using a much simpler and easier to read construct.

Using awk in that manner and piping to sh is very unnecessary and overly complicated.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question