patternbashMinor
Checking if input file exists, assign output file destination, prevent overwriting any files
Viewed 0 times
destinationpreventfileoverwritinganycheckingoutputinputfilesexists
Problem
I would like to use the following routines in my submission script for GAMESS calculations. Before I do, I would like to get some input if I can improve it somehow.
The three messaging routines (
Since I am not posting the whole script due to it's length, I only include the lines that are necessary to run it. I will describe what the routines do in a little more detail below.
The input to the script should accept either a job title or a file name. The corresponding counterpart needs to be found automatically. It then should assign a output file destination based on the supplied (or found) input file ending. It checks weather this file exists and creates a backup.
Based on the job title it also checks if any auxiliar files exist from a previous run. If they could potentially stop the execution the script will abort. If they are in the current directory, they will be backed up like the output file.
Even though it seems unnecessary,
```
#!/bin/sh
# Following Variables will be set by the main script
username="$USER"
requestGamessAuxOutDir="/home/$username/scr"
# Read Input from cmdline
submittedInputfile=$1
#
# Print logging information and warnings nicely.
# If there is an unrecoverable error: display a message and exit.
#
message ()
{
echo INFO : $*
}
warning ()
{
echo WARNING: $*
}
fatal ()
{
echo ERROR : $*
exit 1
}
# files must e
The three messaging routines (
message,warning,fatal) are taken from my earlier review request, although I have chosen a more verbose name.Since I am not posting the whole script due to it's length, I only include the lines that are necessary to run it. I will describe what the routines do in a little more detail below.
The input to the script should accept either a job title or a file name. The corresponding counterpart needs to be found automatically. It then should assign a output file destination based on the supplied (or found) input file ending. It checks weather this file exists and creates a backup.
Based on the job title it also checks if any auxiliar files exist from a previous run. If they could potentially stop the execution the script will abort. If they are in the current directory, they will be backed up like the output file.
Even though it seems unnecessary,
username="$USER" is included because the username from the queue it is sent to may not be equal to the username for the linux system.is_readableFileis used to make the following code more readable.
exist_Fileis the backup procedure.
exist_GamessAuxfileschecks if there are files leftover from a previous (incomplete) run.
validate_GamessFilesis the main part.
- last line is to check if it is actually working. It does (at least in the cases I was able to come up with.)
```
#!/bin/sh
# Following Variables will be set by the main script
username="$USER"
requestGamessAuxOutDir="/home/$username/scr"
# Read Input from cmdline
submittedInputfile=$1
#
# Print logging information and warnings nicely.
# If there is an unrecoverable error: display a message and exit.
#
message ()
{
echo INFO : $*
}
warning ()
{
echo WARNING: $*
}
fatal ()
{
echo ERROR : $*
exit 1
}
# files must e
Solution
Double-quote paths
In many places you correctly enclosed within double quotes the variables containing paths. But in many other places you didn't. You should always enclose these variables.
Naming
Some elements really need better names.
You're using multiple naming schemes, which is unusual. Here are some common naming schemes:
I suggest to adopt one of these and use consistently.
Lastly, it's customary to use plural names for collection types, so it's confusing here to see a plural name for a single value:
BTW, you didn't need to quote those extensions.
Readability
The indentation is a bit inconsistent. I suggest to indent using 4 spaces at every level.
In this code, the double quotes embedded within double quotes are a bit awkward:
This is not a problem, but perhaps you might be interested in the idea of using single quotes instead:
Another nitpick I have here is that the message is a bit of a lie, as the file wasn't really moved yet. I propose this alternative:
Thanks to the verbose flag, the command will print the rename, doing the work for you. I also added the recommended double quotes.
In many places you correctly enclosed within double quotes the variables containing paths. But in many other places you didn't. You should always enclose these variables.
Naming
Some elements really need better names.
exist_File gives the impression that it checks if a file exists (and do nothing else). But the implementation is much more than that, it's more like making a backup. backupIfExists would be better.You're using multiple naming schemes, which is unusual. Here are some common naming schemes:
- camelCase
- snake_case (all lowercase)
I suggest to adopt one of these and use consistently.
Lastly, it's customary to use plural names for collection types, so it's confusing here to see a plural name for a single value:
for possibleAuxfiles in "dat" "trj" "rst" "efp"; doBTW, you didn't need to quote those extensions.
Readability
The indentation is a bit inconsistent. I suggest to indent using 4 spaces at every level.
In this code, the double quotes embedded within double quotes are a bit awkward:
message "File \"$1\" exists."
message "It has been moved to \"$1.$filecount\"."
mv $1 $1.$filecountThis is not a problem, but perhaps you might be interested in the idea of using single quotes instead:
message "File '$1' exists."
message "It has been moved to '$1.$filecount'."
mv $1 $1.$filecountAnother nitpick I have here is that the message is a bit of a lie, as the file wasn't really moved yet. I propose this alternative:
message "File '$1' exists, will be moved"
mv -v "$1" "$1.$filecount"Thanks to the verbose flag, the command will print the rename, doing the work for you. I also added the recommended double quotes.
Code Snippets
for possibleAuxfiles in "dat" "trj" "rst" "efp"; domessage "File \"$1\" exists."
message "It has been moved to \"$1.$filecount\"."
mv $1 $1.$filecountmessage "File '$1' exists."
message "It has been moved to '$1.$filecount'."
mv $1 $1.$filecountmessage "File '$1' exists, will be moved"
mv -v "$1" "$1.$filecount"Context
StackExchange Code Review Q#124354, answer score: 2
Revisions (0)
No revisions yet.