Alias is a very useful function that allows you to define shortcuts to frequently used commands, without the need for long parameters. Once you have defined an alias, you just need to call the selected name to replace the selected command (or command string).
We define the Aliasy in the file .bash_profile
. This is a text file in the user's home directory. This file is loaded by the shell after each login to the system.
Here are a few examples of used aliases:
alias dir='/usr/bin/ls -l'
abcd@ui ~>/usr/bin/ls
bin myjob.sh scripts
abcd@ui ~ $ dir
drwxr-xr-x 2 abcd kdm 2048 Aug 24 11:31 bin
-rwxr-xr-x 1 abcd kdm 25 Aug 30 14:43 myjob.sh
drwxr-xr-x 2 abcd kdm 2048 Aug 31 08:54 scripts
If we often check the status of our tasks and do not want to enter each time squeue -u user , alias alias sq='squeue -u ${USER}'
may replace the entire command.
abcd@ui ~ $ squeue
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
578372 short zad33 user1 PD 0:00 1 (QOSMaxJobsPerUserLimit)
578371 normal zad1 abcd PD 0:00 1 (QOSMaxJobsPerUserLimit)
578370 normal zad2 abcd PD 0:00 1 (QOSMaxJobsPerUserLimit)
578372 short job9a user3 PD 0:00 1 (QOSMaxJobsPerUserLimit)
...
abcd@ui ~ $ sq
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
578371 normal zad1 abcd PD 0:00 1 (QOSMaxJobsPerUserLimit)
578370 normal zad2 abcd PD 0:00 1 (QOSMaxJobsPerUserLimit)
Command rm file is used to delete files and directories rm-r directory_name.
abcd@ui ~ $ ls
myjob.sh myrus.sh temp wyniki.578371 wyniki.578372
abcd@ui ~ $ rm wyniki.*
abcd@ui ~ $ ls
myjob.sh myrus.sh temp
Defining an alias alias rm='/usr/bin/rm -i'
, we cause the command rm this will require confirmation of the deletion of the file. This allows you to prevent accidental deletion of the necessary files.
abcd@ui ~ $ ls
myjob.sh myrus.sh temp wyniki.578371 wyniki.578372
abcd@ui ~ $ rm wyniki.578371
/usr/bin/rm: remove regular file 'wyniki.578371'? n
abcd@ui ~ $ ls
myjob.sh myrus.sh temp wyniki.578371 wyniki.578372
Similar, defining similar aliases for commands cp and mv, you can prevent overwriting files when running these commands.
Alias can be avoided by giving full weight to the command. For example, command /usr/bin/rm This will cause you to rm will not ask for confirmation before deleting files.
The following example shows the use of environmental variables of the system and the SLURM queue system.
An example of the script myrun.sh
#!/bin/bash
# Check if a file name to execute is provided
if [[ -z $1 ]]; then
echo "No file name provided for execution"
exit 1
fi
# Check if the provided file exists and we have access to it
if [[ ! -f $1 ]]; then
echo "The provided file ($1) does not exist"
exit 1
fi
# Working directory where results will be stored /home/<username>/<job_number>
WORKDIR=/home/${USER}/${SLURM_JOBID}
# Check if the working directory exists, if not, create it
if [[ ! -d ${WORKDIR} ]]; then
/usr/bin/mkdir ${WORKDIR} || exit 1
fi
# Call the program or script to execute
$1 > ${WORKDIR}/results.${SLURM_JOB_NAME}
Here is an example of how to use the previous script to run a task myjob.sh
abcd@ui ~ $ srun myrun.sh myjob.sh
Scripts in the directory
/home/${USER}/bin
can be started by typing only the name of the script.