Solved

Shell script runing error when using variable

Posted on 2012-03-29
9
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 37780886
>>cat: cannot stat -

is the only error u get?
0
 
LVL 10

Expert Comment

by:pfrancois
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 84

Accepted Solution

by:
ozo earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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 …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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