Solved

Shell script runing error when using variable

Posted on 2012-03-29
9
576 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Register AutoHotkey 12 72
AWS- KeepAlived notify script not working 23 82
linux pull logs and export FTP 4 71
t-sql sp_addlinkedserver possible risks 3 69
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

773 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