Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep variable replacement error - can't open file

Status
Not open for further replies.

tekinfarmland

Technical User
May 22, 2002
20
US
this command works
CSTIME=$(grep "PA3.1" Jobcheck.tbl | cut -f2 -d',')

but when I replace the values with variables with same values
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')

I get "error can't open Jobcheck.tbl"

Am I missing special characters in the 2nd grep command?
Any help would be appreciated.
 
Maybe if you echo $JNAME and $TBL in your script you may find that you are not getting the value you expect in the variables ?

Or maybe try wrapping the $JNAME in quotes as you have in the top example?

IHTH

Laurie
 
On AIX 5.3 TL12 SP5 using ksh93 Version M-12/28/93e

Code:
# cat jobcheck.tbl
a,PA3.1,c,d
b,PA1.2,c,d
c,PA3.1,d,z

# cat d.sh
#!/bin/ksh93

TBL="/tmp/jobcheck.tbl"
JNAME="PA3.1"
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')
echo $CSTIME

# ./d.sh
PA3.1 PA3.1
 
Code:
$ cat /etc/*release
Red Hat Enterprise Linux ES release 4 (Nahant Update 9)

$ cat jobcheck.tbl
a,PA3.1,c,d
b,PA1.2,c,d
c,PA3.1,d,z

$ cat d.sh
#!/bin/ksh

TBL="./jobcheck.tbl"
JNAME="PA3.1"
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')
echo $CSTIME

$ ./d.sh
PA3.1 PA3.1
 
Code:
# cat /etc/release
                        Solaris 8 6/00 s28s_u1wos_08 SPARC
           Copyright 2000 Sun Microsystems, Inc.  All Rights Reserved.
                             Assembled 26 April 2000

# cat jobcheck.tbl
a,PA3.1,c,d
b,PA1.2,c,d
c,PA3.1,d,z

# cat d.sh
#!/bin/ksh

TBL="./jobcheck.tbl"
JNAME="PA3.1"
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')
echo $CSTIME

# ./d.sh
PA3.1 PA3.1
 
tekinfarmland, what shell, operating system and versions of both are you using?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Been away from this for awhile. My last work with unix shell was on HPUX about 8 years ago. How can I tell what shell version? OS is Solaris 10 10/09 s10s_u8wos_08a SPARC
 
Well, just knowing which shell it is would be enough, i.e. the output of ps -fp $$.

Does your test like the ones by blarneyme above still yield different results?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
looks like it is ksh
$ ps -fp $$
UID PID PPID C STIME TTY TIME CMD
leroy 3625 3633 0 06:34:49 pts/1 0:00 -ksh
 
Anyway, why not simply this ?
Code:
CSTIME=$(awk -F, "/$JNAME/"'{print $2}' $TBL)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
no, the awk version did not work.
"awk: can't open ./Jobcheck.tbl"
 
Seems like Jobcheck.tbl is not in the current directory.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
exactly that is the odd problem. Jobcheck.tbl IS in the same directory as the the file executing (Jobcheck.ksh). and both files are set to 7777.
 
I'd put a cd command in Jobcheck.ksh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What's the ownership and permissions on [tt]jobcheck.tbl[/tt]?

Look for non-printable characters in the file name. Try this...
Code:
ls -lb *jobcheck.tbl*

Did you create or edit any of your scripts on a PC? If so, this can introduce extra EOL characters at the end of lines. This can mess things up. If you edit the file in [tt]vi[/tt] you should be able to see them. Or use the [tt]unix2dos[/tt] command to remove them.
 
all good replies. I seem to have isolated the issue down to just the grep part.
original command:
JNAME=PA3.1
TBL=Jobcheck.tbl
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')

simple command:
Jobcheck.tbl contains "PA3.1,0,0,0,20"
echo $JNAME
CSTIME=$(grep $JNAME Jobcheck.tbl)
I echo $JNAME before the command and receive "PA3.1"
After, I echo $CSTIME and receive an empty string, like grep did not find anything. Is there something I am missing in my grep statement?

 
Code:
# ./d.sh
JNAME PA3.1
CSTIME 0
# cat d.sh
#!/bin/ksh

TBL=./jobcheck.tbl
JNAME=PA3.1
echo JNAME $JNAME
CSTIME=$(grep $JNAME $TBL | cut -f2 -d',')
echo CSTIME $CSTIME
# ./d.sh
JNAME PA3.1
CSTIME 0
# cat /etc/release
                        Solaris 8 6/00 s28s_u1wos_08 SPARC
           Copyright 2000 Sun Microsystems, Inc.  All Rights Reserved.
                             Assembled 26 April 2000
#
Two things.

1) Why don't you do what I have done and copy and paste what jobcheck.tbl contains and the contents of your script and the output of execution of the script?

2) After that, run your script with:
Code:
ksh -x ./myscript.ksh
So you can trace the execution.
 
Issue solved. my command now works in the script.
CSTIME=$(grep "$JNAME" "$TBL" | cut -f2 -d',')

My issue was unseen characters caused by the windows editor.
I removed them by using "dos2unix temp1.ksh temp2.ksh"

Thanks to everyone that replied.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top