Solved

Shell script runing error when using variable

Posted on 2012-03-29
9
583 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

632 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