Solved

tcsh shell script cannot open newly created file

Posted on 2007-03-21
30
1,331 Views
Last Modified: 2013-12-26
in a tcsh script, I created a new file in a different directory. in the same script in the later part, I tried to open the file I just created, it returned "cannot open...."

I've checked everything and positive that the path is correct. Now if I exit the script and run the same command, then the file opens OK

My suspect is that because the file is newly created so there maybe some delay and the script cannot recognize it.

Can you help ?
0
Comment
Question by:Cee Vee
  • 13
  • 10
  • 6
  • +1
30 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 18768414
What command you are using to create and open the file? Show us some code. If the delay is the cause of the problem as you suspect, you could put some time delay by sleep command.

CreateFile
#Delay 5 seconds...
sleep 5
OpenFile
0
 
LVL 84

Expert Comment

by:ozo
ID: 18768415
Did you close the file that you created?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18768424
Some code would be extremely useful.
0
 

Author Comment

by:Cee Vee
ID: 18768430
Here is part of the script

        echo "osfetch_linux $default_ip $filename"

        # Fetch data from Musti box
        osfetch_linux $default_ip $filename

        # Split it up
        set split_dir = $filename'_'$chunk_size
        mkdir $split_dir
        split -b $chunk_size $filename $split_dir/$filename

        # List available splitted chunks for user to select
        set ii = 1
        set splitted_chunks = `ls $split_dir`
        foreach item ( `ls $split_dir` )
            if( ! -d "$1/$item" ) then
                set splitted_chunks[$ii] = $item
                echo $ii - $item
                @ ii++
            endif
        end

        # User selection        
        echo -n "Enter chunk # to translate: "
        set inputline = $<
        set chunk_to_translate = $splitted_chunks[$inputline]
        set chunk_to_translate = $split_dir/$chunk_to_translate  
        echo "chunk_to_translate == <$chunk_to_translate>"
       
        # Translate it    
        ibuprofen_linux $chunk_to_translate
0
 

Author Comment

by:Cee Vee
ID: 18768448
I tried to wait for sometimes before making the selection, but still same result "File not found or file cannot be opened." .... as soon as I exit the script, and run it again (hard code exact filename printed out to terminal just to test), then the file opens ok.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768471
Does this line cause error?

ibuprofen_linux $chunk_to_translate

if so, add

echo "----------"
echo $ii
echo $split_dir
echo $chunk_to_translate
ls -l $split_dir
ls -l $chunk_to_translate
echo "----------"

before it. What do you get?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18768477
does ibuprofen_linux try to open any other files besides $chunk_to_translate?
0
 

Author Comment

by:Cee Vee
ID: 18768484
--> amit_q : this is what came out

Max received packet size was 18824.
Data bytes received 5997416
Speed 5856.85 KB/s
1 - t6aa
2 - t6ab
3 - t6ac
4 -
Enter chunk # to translate: 2
chunk_to_translate == <t6_2000k/t6ab>
----------
5
t6_2000k
t6_2000k/t6ab
total 5384
-rw-rw-r--    1 kevo     sw1       2048000 Mar 21 17:46 t6aa
-rw-rw-r--    1 kevo     sw1       2048000 Mar 21 17:46 t6ab
-rw-rw-r--    1 kevo     sw1       1901416 Mar 21 17:46 t6ac
ls: t6_2000k/t6ab: No such file or directory
Exit 1
----------
File not found or file cannot be opened.
Exit 255
0
 

Author Comment

by:Cee Vee
ID: 18768489
--> ozo : no it doesn't open any other file

FYI : If i don't create the new directory, and not spliting the file....it works fine
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768532
How long does split command take? Is script blocked when the split is happening? You can check by adding date command before and after split command

date
split -b $chunk_size $filename $split_dir/$filename
date
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768538
and inanimately after the script throws error what do you get if you do

ls -l t6_2000k/t6ab

Use the same filename that threw the error as soon as possible.
0
 

Author Comment

by:Cee Vee
ID: 18768539
--> amit_q : putting date around split command resulted

Speed 6613.22 KB/s
Wed Mar 21 17:59:31 PDT 2007
Wed Mar 21 17:59:32 PDT 2007
1 - t8aa
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768547
Try to put a larger sleep (say a min or so). A 5Gb file getting splitted in 1 seconds with 2 new files of 2 GB and 1 new file of 1 GB doesn't seem right. It has to take more than 1 seconds. What o/s you have?

Also as soon as you get the error, do ls -l of the same file that caused error. You probably would get the same error if you execute the ls -l soon enough after the completion (failure) of the script.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768549
Nevermind, it is 5MB not GB as I misunderstood.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18768557
try
echo "chunk_to_translate == <$chunk_to_translate>" | od -c
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Cee Vee
ID: 18768562
--> amit_q : it's 5MB not 5GB, and you're right, ls also returns fail

chunk_to_translate == <t1_2000k/t1ab>
File not found or file cannot be opened.
Exit 255
ls: t6_2000k/t6ab: No such file or directory
Exit 1

is there anything wrong with my string $chunk_to_translate ? maybe any hidden chracter even though i didn't see it in echo
0
 

Author Comment

by:Cee Vee
ID: 18768566
--> ozo : here is the output

Speed 5149.01 KB/s
Wed Mar 21 18:09:14 PDT 2007
Wed Mar 21 18:09:15 PDT 2007
1 - t2aa
2 - t2ab
3 - t2ac
4 -
Enter chunk # to translate: 2
0000000   c   h   u   n   k   _   t   o   _   t   r   a   n   s   l   a
0000020   t   e       =   =       <   t   2   _   2   0   0   0   k   /
0000040 033   [   0   0   m   t   2   a   b 033   [   0   0   m   >  \n
0000060
File not found or file cannot be opened.
Exit 255
0
 

Author Comment

by:Cee Vee
ID: 18768574
is the "\n" throw me off ? if so, how do I get rid of it ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18768580
I suspect  the 033   [   0   0   m may be be part of the problem
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768583
\n should not be problem, there is something weired in the beginning though.
0
 

Author Comment

by:Cee Vee
ID: 18768586
don't be alarmed by the prefix name such as "t1, t2, t3 ....etc"

I specified that name.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768588
Could you also do

ls t6_2000k | od -c
0
 

Author Comment

by:Cee Vee
ID: 18768589
--> ozo : I agree with you. Where are those 033 [...etc comes from ? how to get rid of it ?
0
 

Author Comment

by:Cee Vee
ID: 18768597
--> amit_q : here is the result

Speed 5567.98 KB/s
Wed Mar 21 18:19:01 PDT 2007
Wed Mar 21 18:19:02 PDT 2007
1 - t3aa
2 - t3ab
3 - t3ac
4 -
Enter chunk # to translate: 2
0000000   c   h   u   n   k   _   t   o   _   t   r   a   n   s   l   a
0000020   t   e       =   =       <   t   3   _   2   0   0   0   k   /
0000040 033   [   0   0   m   t   3   a   b 033   [   0   0   m   >  \n
0000060
ls: t6_2000k: No such file or directory
0000000 033   [   0   0   m 033   [   m   E   x   i   t       1  \n
0000017
Exit 1
File not found or file cannot be opened.
Exit 255
ls: t6_2000k/t6ab: No such file or directory
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 18768603
ls may be outputing colorized output, although it is usually turned off when the output is not a terminal
0
 
LVL 58

Assisted Solution

by:amit_g
amit_g earned 250 total points
ID: 18768606
There seems to be a little bug in the script.

if( ! -d "$1/$item" ) then

should be

if( ! -d "$split_dir/$item" ) then

although this has no bearing on the problem you are facing now.

>>ls: t6_2000k/t6ab: No such file or directory

You don't have to execute it in the script. you can do it from command line after the script has thrown error and finished executing. Also use the currently created dir. for example in the past run it should have been

ls t3_2000k | od -c
0
 
LVL 84

Expert Comment

by:ozo
ID: 18768628
can you show your
man ls
0
 

Author Comment

by:Cee Vee
ID: 18768640
--> ozo : you're absolutely right. it's the color that threw me off.
Brilliant !

thanks both of you for your time on this. Thanks to amit_q for pointing out the bug. I'll split the points.
0
 

Author Comment

by:Cee Vee
ID: 18768646
for the record : I have my ls aliases to "ls-F --color"
0
 
LVL 58

Expert Comment

by:amit_g
ID: 18768671
That was a great catch ozo.

Add

unalias *

in the beginning of your script to remove all aliases for the script.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

18 Experts available now in Live!

Get 1:1 Help Now