This is the second part of this series of notes on the advanced use of the Unix shell. The first part can be accessed by following the link. In this second part, I will show some advanced examples I wrote for my research activity.
1. CHANGE FILE NAMES SCRIPT
I wrote this script to change the default names of the files generated during a molecular dynamics simulation by the program mdrun in the early version of the GROMACS package for MD simulation. GROMACS is a widely used software package for simulating the behavior of molecules and molecular systems. Although it is now possible to define the root name of the files upon running the simulation program, this script can still be helpful. The script is written in the C Shell (csh) .
#! /bin/csh -ef
#
# Change the names of the default output Files
# generated by the program GROMACS mdrun program
#
# (c) Danilo Roccatano
#######
#
#Define the radix of the files name
#
setenv RADIX 'EH_min'
if (-e md.log) then
if ( -e $RADIX'.log') then
echo Warning $RADIX'.log' exist
else
mv md.log $RADIX'.log'
endif
else
echo The md.log file does not exist!
endif
if (-e ener.ene) then
if ( -e 'e'$RADIX'.ene') then
echo Warning 'e'$RADIX'.ene' exist
else
mv ener.ene 'e'$RADIX'.ene'
endif
else
echo The energy file does not exist
endif
if (-e ctraj.xtc ) then
if ( -e 'r'$RADIX'.xtc') then
echo Warning 'r'$RADIX'.xtc' exist
else
mv ctraj.xtc 'r'$RADIX'.xtc'
endif
else
echo The compressed trajectory file does not exist
endif
if (-e confout.gro) then
if ( -e 'x'$RADIX'.gro') then
echo Warning 'x'$RADIX'.gro' exist
else
mv confout.gro 'x'$RADIX'.gro'
endif
else
echo The final configuration file does not exit.
echo Probably your simulation crashed before the end
endif
exit
Here’s a breakdown of what the script does:
setenv RADIX 'EH_min': This sets the environment variableRADIXto the string'EH_min'. TheRADIXvariable is used as a prefix for the new file names.- The script then checks the existence of several default output files generated by GROMACS and renames them with the specified
RADIXprefix if they exist. Here are the checks and actions performed for each file:
md.log: Ifmd.logexists, it renames it to'EH_min.log'unless'EH_min.log'already exists.ener.ene: Ifener.eneexists, it renames it to'eEH_min.ene'unless'eEH_min.ene'already exists.ctraj.xtc: Ifctraj.xtcexists, it renames it to'rEH_min.xtc'unless'rEH_min.xtc'already exists.confout.gro: Ifconfout.groexists, it renames it to'xEH_min.gro'unless'xEH_min.gro'already exists.
- For each renaming action, if the target file with the new name already exists, it prints a warning message indicating that the file already exists.
- If any of the original files (
md.log,ener.ene,ctraj.xtc,confout.gro) do not exist, it prints a message indicating that the file does not exist. - Finally, there’s an exit statement, which signifies the end of the script.
If you save the script as rename_files.csh, and you want to use it to rename the default output files generated by GROMACS with the prefix 'EH_min'. You would run the script from the command line as follows:
./rename_files.csh
Assuming that GROMACS-generated output files like md.log, ener.ene, ctraj.xtc, and confout.gro exist in the same directory as the script, and you haven’t used the script before, the script will perform the following actions:
- If
md.logexists, it will rename it to'EH_min.log'unless'EH_min.log'already exists. - If
ener.eneexists, it will rename it to'eEH_min.ene'unless'eEH_min.ene'already exists. - If
ctraj.xtcexists, it will rename it to'rEH_min.xtc'unless'rEH_min.xtc'already exists. - If
confout.groexists, it will rename it to'xEH_min.gro'unless'xEH_min.gro'already exists.
The script will provide warning messages if any of the new file names already exist to avoid accidental overwriting.
After running the script, your files will be renamed with the 'EH_min' prefix. For example, md.log will become 'EH_min.log', ener.ene will become 'eEH_min.ene', ctraj.xtc will become 'rEH_min.xtc', and confout.gro will become 'xEH_min.gro'.
Please make sure to backup your original files or use this script in a controlled environment, as renaming files can potentially result in data loss if not used carefully.
Project Directory Makers
This K-shell script generates a set of subdirectories in which an MD project can be started. The script prompts for the project’s name and then generates a subset of standard subdirectories in which the MD-generated files will be stored.
#!/bin/sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# This K-shell script generate a set of subdirectory in which a MD
# project can be started.
# The script inquire for the name of the project and then generate a
# subset of standard subdirectories in which the MD generated files
# will be stored.
#
# (c) Danilo Roccatano
#
# ==> Version 1.0 25 Aug 1998
#
########################################################################
#
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >| tmpfile
echo "% PIF: Project Information File" >> tmpfile
echo "% " >> tmpfile
echo "% Created by user : " $USER >> tmpfile
echo "% Date : " `date` >> tmpfile
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" >> tmpfile
#
# Prepare a the PIF file
#
echo "documentclass[a4paper,12pt,dvips]{article}" >> tmpfile
echo "\begin{document}" >> tmpfile
clear;
cat << EOF
Insert the full project title
These information will be added to the Project Info File (PIF)
(Write "." at prompt to bail out)
EOF
FIRSTCH=""
echo "\title{" >> tmpfile
while [ "$FIRSTCH" != "." ]; do
echo $COMM >> tmpfile
echo "Title: "
read -r COMM;
typeset FIRSTCH=$COMM
done
echo "}" >> tmpfile
while [ 0 ]; do
clear;
cat << EOF
Insert the code name of the MD project you want to start.
This code name will be used to name the root directory of the
project, and it will be also the alias name to those directory.
EOF
echo "Code Name ? "
read CODE;
if [ "$CODE" != "" ]; then
break;
fi
done
while [ 0 ]; do
clear;
cat << EOF
The Code Name is :
EOF
echo $CODE
echo "Please Confirm (Y/N) "
read CONF;
if [ "$CONF" = "y" -o "$CONF" = "Y" ]; then
break;
fi
if [ "$CONF" = "n" -o "$CONF" = "N" ]; then
exit;
fi
done
clear;
echo "% Project Code Name: " $CODE >> tmpfile
echo "\abstract{" >> tmpfile
cat << EOF
Insert a comment describing the aim of the project
These information will be added to the Project Info File (PIF)
(Write "." at prompt to bail out)
EOF
FIRSTCH=""
COMM=""
while [ "$FIRSTCH" != "." ]; do
echo $COMM >> tmpfile
echo "Comment: "
read COMM;
typeset FIRSTCH=$COMM
done
echo "}" >> tmpfile
clear;
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Start the creation of the Root directory and subdirectories
# Subdirectories names:
# * Trj : Here will be stored the *xtc and *trj files
# * Ene : " " *ene
# * Crd : " " *gro
# * Log : " " *log and PIF
# * Run : Files required to perform simulations
# * Ana : Outputs from the Analysis of trajectories
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LDIR="Trj Ene Crd Log Run Ana"
set ${PROJPATH:=`pwd`}
while [ 0 ]; do
echo "Current directory:"
echo $PROJPATH
echo "Do you want Install here the project directories? "
read CONF;
if [ "$CONF" = "y" -o "$CONF" = "Y" -o "$CONF" = "N" -o "$CONF" = "n" ]; then
break;
fi
done
if [ "$CONF" = "N" -o "$CONF" = "n" ]; then
echo "Insert the path name of the directory where you want to install"
echo " the project directories:"
read PROJPATH;
if [ "$PROJPATH" != "" ]; then
break;
fi
fi
echo "%%%" >> tmpfile
echo "% Project Main directory path:">> tmpfile
echo "% " $PROJPATH/$CODE >> tmpfile
echo "%%%" >> tmpfile
#
#==========================================================================
#
mkdir $PROJPATH/$CODE
cd $PROJPATH/$CODE
for LISTD in $LDIR; do
mkdir $LISTD
done
clear;
echo "Created the Project directory:"
echo
echo $PROJPATH/$CODE
echo
echo "and the following subdirectories:"
for LISTD in $LDIR; do
echo $LISTD
done
for LISTD in $LDIR; do
typeset TMP=$LISTD
echo "GX"$TMP"="$PROJPATH"/"$LISTD >> tmpdir1
done
cd $PROJPATH/
echo "\end{document}" >> tmpfile
#echo $LDIR[4]
# cp tmpdir $PROJPATH/$LDIR{4}
exit;
FURTHER READINGS
- Shelley Powers, Jerry Peek, Tim O’Reilly, Mike Loukides. Unix Power Tools, Third Edition. O’Reilly Media, Inc.; 3rd edition (October 1, 2002)
- David I. Schwartz. Introduction to UNIX (2nd Edition)

Pingback: Unix C-Shell Programming Notes. Part I |