tcsh shell script cannot open newly created file

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 ?
Cee VeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
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
ozoCommented:
Did you close the file that you created?
0
TintinCommented:
Some code would be extremely useful.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Cee VeeAuthor Commented:
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
Cee VeeAuthor Commented:
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
amit_gCommented:
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
ozoCommented:
does ibuprofen_linux try to open any other files besides $chunk_to_translate?
0
Cee VeeAuthor Commented:
--> 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
Cee VeeAuthor Commented:
--> 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
amit_gCommented:
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
amit_gCommented:
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
Cee VeeAuthor Commented:
--> 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
amit_gCommented:
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
amit_gCommented:
Nevermind, it is 5MB not GB as I misunderstood.
0
ozoCommented:
try
echo "chunk_to_translate == <$chunk_to_translate>" | od -c
0
Cee VeeAuthor Commented:
--> 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
Cee VeeAuthor Commented:
--> 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
Cee VeeAuthor Commented:
is the "\n" throw me off ? if so, how do I get rid of it ?
0
ozoCommented:
I suspect  the 033   [   0   0   m may be be part of the problem
0
amit_gCommented:
\n should not be problem, there is something weired in the beginning though.
0
Cee VeeAuthor Commented:
don't be alarmed by the prefix name such as "t1, t2, t3 ....etc"

I specified that name.
0
amit_gCommented:
Could you also do

ls t6_2000k | od -c
0
Cee VeeAuthor Commented:
--> ozo : I agree with you. Where are those 033 [...etc comes from ? how to get rid of it ?
0
Cee VeeAuthor Commented:
--> 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
ozoCommented:
ls may be outputing colorized output, although it is usually turned off when the output is not a terminal
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amit_gCommented:
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
ozoCommented:
can you show your
man ls
0
Cee VeeAuthor Commented:
--> 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
Cee VeeAuthor Commented:
for the record : I have my ls aliases to "ls-F --color"
0
amit_gCommented:
That was a great catch ozo.

Add

unalias *

in the beginning of your script to remove all aliases for the script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.