BASH Corrupts Files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH Corrupts Files?
# 1  
Old 04-30-2015
Hammer & Screwdriver BASH Corrupts Files?

I have a system that uses file access to manage parallel processes. Each process checks to see if a file with a unique name that corresponds to the job it's got to do exists. If it does, it exits. If not, it creates one with that name. There is also a background process that is writing to this file.

Would this cause only blocking, or would it cause actual disk corruption?

Thx!
# 2  
Old 04-30-2015
There's an instant between 'check' and 'create file' when something else could create that file without bash knowing.

You could use mkdir. That's atomic. The first time it gets called it succeeds and creates the folder, the second time, it fails, nothing can sneak between.

I think there's a way for bash to try and create a file and report error if it already exists but am struggling to find the syntax.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-30-2015
I found it. It's a bash-only feature, mind.

Code:
set -o noclobber

if ! exec 5>filename
then
        echo "Someone else already has filename" >&2
        exit 1
fi

# things writing to FD 5

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-30-2015
That isn't a bash ism. The set -C (aka set -o noclobber) option and its interaction with the:
Code:
fd> file

and:
Code:
fd>| file

redirection operators is required in all POSIX conforming shells...
Code:
#!/bin/bash
options=$(set +o)	# Save current shell options
set -C			# Set noclobber option (synonym for "set -o noclobber"
if > xyzzy		# Fail if file already exists
then	echo '> xyzzy succeeded'
else	echo '> xyzzy failed'
fi
if > xyzzy		# Fail if file already exists
then	echo '2nd > xyzzy succeeded'
else	echo '2nd > xyzzy failed'
fi
if >| xyzzy		# Succeed even if file already exists (as long as you
			# have write permission)
then	echo '>| xyzzy succeeded'
else	echo '>| xyzzy failed'
fi
$options		# Reset original shell options
rm -f xyzzy		# Remove test file

produces output similar to:
Code:
> xyzzy succeeded
tester: line 8: xyzzy: cannot overwrite existing file
2nd > xyzzy failed
>| xyzzy succeeded

I said similar to because the format of the diagnostic message can vary slightly from shell to shell and system to system.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-01-2015
This sounds great! Thanks!

I'm not sure I understand the mkdir command, tho', since I'm talking about creating
a file, not a folder. I'll have to look into this and see how it pertains to files.

---------- Post updated at 11:37 PM ---------- Previous update was at 11:26 PM ----------

As a matter of fact, let me ask another question. I've set this program up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that contains individual files for each type of information would be wasteful of storage and slower to retrieve. I realize now that I've only assumed this and was not sure.

How would the method of having a thousand or so directories with individual files inside each to contain several types of information compare to my current method of files with those same unique names containing fields to be extracted from those files?

Would getting the file inside a directory be less resource/time intensive than having to extract each piece of info from a single file with that unique name?

?????????????
# 6  
Old 05-01-2015
Quote:
Originally Posted by gmark99
This sounds great! Thanks!

I'm not sure I understand the mkdir command, tho', since I'm talking about creating
a file, not a folder. I'll have to look into this and see how it pertains to files.
You have been given two suggestions using set -C or set -o noclobber that have nothing to do with mkdir. Have you looked at them?
Quote:
Originally Posted by gmark99
---------- Post updated at 11:37 PM ---------- Previous update was at 11:26 PM ----------

As a matter of fact, let me ask another question. I've set this program up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that contains individual files for each type of information would be wasteful of storage and slower to retrieve. I realize now that I've only assumed this and was not sure.

How would the method of having a thousand or so directories with individual files inside each to contain several types of information compare to my current method of files with those same unique names containing fields to be extracted from those files?

Would getting the file inside a directory be less resource/time intensive than having to extract each piece of info from a single file with that unique name?

?????????????
I don't see how this has anything to do with the topic of this thread. Trying to discuss two topics in one thread confuses everyone trying to help you.

If you want help with another topic, please start a new thread to discuss it and give a much clearer example/explanation of the two proposals you want to evaluate. Show us the contents of the file(s) in the directories and the contents of the individual file(s) if the data is all in one directory (using CODE tags) and show us what data you are trying to extract from that file or those files.

The topic of discussion for this thread is performing an atomic check for the existence of a regular file and creating it if it did not already exist.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-01-2015
If different processes write to one file at a time, then there is a risk of file corruption (but not filesystem corruption). The corruption is usually that one of the concurrent writes is simply lost. Then it's better to have separate files e.g. in separate directories.
Regarding disk load, it would be best to have a memory filesystem, a RAM disk or the /tmp in Solaris (tmpfs).
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Looking into files question

I have a bunch of files that are messages in my directory. Each message has a date located in the file. How can I look into each file and find the date? Thank you for any help (7 Replies)
Discussion started by: totoro125
7 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

Bash: join 2 files

Hello ! I want to join 2 files. They look like this: KE340296.1 1 0/0:11 KE340296.1 2 0/0:12 KE340296.1 6 0/1:13 KE340297.1 1 0/1:14 KE340297.1 3 0/1:15 KE340297.1 4 0/1:16 and KE340296.1 1 0/0:21 KE340296.1 2 0/0:22 KE340296.1 3... (4 Replies)
Discussion started by: MumuGB
4 Replies

4. UNIX for Dummies Questions & Answers

Bash script to sort files

I've got a disorganized list of items and quantities for each. I've been using a combination of grep and sort to find out how much to buy of each item. I'm tired of having to constantly using these commands so I've been trying to write a shell script to make it easier, but I can't figure out how... (3 Replies)
Discussion started by: PTcharger
3 Replies

5. Shell Programming and Scripting

LFTP corrupts special characters

Hi, I am trying to use lftp to mirror two directories: one on my windows pc and one on a zOS system. One file within the local directory has special characters for different languages, e.g. pou¶ít (czech). When I run lftp, the characters are incorrect. I am transferring in ASCII mode, and the... (5 Replies)
Discussion started by: adam.wis
5 Replies

6. Shell Programming and Scripting

Using bash to separate files files based on parts of a filename

Hey guys, Sorry for the basic question but I have a lot of files that I want to separate into groups based on filenames which I can then cat together. Eg I have: (a_b_c.txt) WB34_2_SLA8.txt WB34_1_SLA8.txt WB34_1_DB10.txt WB34_2_DB10.txt WB34_1_SLA8.txt WB34_2_SLA8.txt 77_1_SLA8.txt... (1 Reply)
Discussion started by: Breentax
1 Replies

7. Shell Programming and Scripting

[bash] Parse files

Hi I've 2 folder A and B, they have files with the same name but different content. I mean A contain---------> aa.txt, bb.txt, cc.txt B contain---------> aa.txt, bb.txt, cc.txt but aa.txt in A has different content from aa.txt in B. I'd like to parse the homonyms files in... (7 Replies)
Discussion started by: Dedalus
7 Replies

8. UNIX for Advanced & Expert Users

Secure FTP corrupts file

Sun Microsystems Inc. SunOS 5.9 I am hoping someone has come across this before. I have a script that transfers several gz files via Secure FTP across to an SFTP server on an NT machine. The transfers show as successful: pack12_200812160337.tar.gz | 768kB | 768kB/s | ETA: 00:00:01 | 37%... (5 Replies)
Discussion started by: ronnie_uk
5 Replies

9. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies

10. Shell Programming and Scripting

.bash files

i have been interacting a little with the forum in the last couple of days and to tell the truth I have learnt quite a bit from some of the posts that I have read accessed or posted.. from some of the books that I have read I have a pretty good idea about shells, profile files, .bashrc, etc, etc... (4 Replies)
Discussion started by: moxxx68
4 Replies
Login or Register to Ask a Question