Solved

Shell script runing error when using variable

Posted on 2012-03-29
9
574 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:sedgwick
Comment Utility
>>cat: cannot stat -

is the only error u get?
0
 
LVL 10

Expert Comment

by:pfrancois
Comment Utility
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
Comment Utility
yes only this error
0
 

Author Comment

by:trazodone
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:trazodone
Comment Utility
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
Comment Utility
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 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much :)
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now