Using BASH to read Keyboard Input

Sometimes, you may want to write a simple BASH menu system to help non-Linux users. Here is a sample sample.

#!/bin/bash
clear
echo "

Welcome to GCluster User Help Menu System
===============================================

Please select:
---------------------------------------------------
1. Display Current Logged Session(s)
2. Display Current Home Space Utilisation
3. Check Running Process(s)
4. Check my Project Funds
5. Check for ABAQUS Licenses
6. Check for MATLAB Distributed Compute Licenses
7. Check for ANSYS Licenses
8. List running jobs from Project Members
0. Quit
---------------------------------------------------
"

read -p "Enter selection [0-8] > "

if [[ $REPLY =~ ^[0-8]$ ]]; then
        if [[ $REPLY == 0 ]]; then
                echo "Program Terminated."
                exit
        fi
        if [[ $REPLY == 1 ]]; then
                echo "Logged session: "
                w |grep $USER
                exit
        fi
        if [[ $REPLY == 2 ]]; then
                echo "Home Space Utilisation ($USER):"
                echo "(Please be patient.... It will take a while)"
                du -sh $HOME
                exit
        fi
        if [[ $REPLY == 3 ]]; then
                echo "Check my Running Processes on Login Node:"
                top -u $USER
                exit
        fi
        if [[ $REPLY == 4 ]]; then
                module load ams
                read -p "Enter your Project ID: " project_id
                project_summary $project_id
                exit
        fi
	if [[ $REPLY == 5 ]]; then
		clear
		check_license_abaqus.sh
		exit	
	fi
	if [[ $REPLY == 6 ]]; then
		clear
		check_license_matlab.sh
		exit
	fi
	if [[ $REPLY == 7 ]]; then 
		clear
		check_license_ansys.sh
		exit
	fi
	if [[ $REPLY == 8 ]]; then
		read -p "Enter your Project ID: " project_id
	 	countjobs=`qselect -P $project_id |wc -l`
		if [[ $countjobs -gt 0 ]]; then
			qstat -ans `qselect -P $project_id`
			exit
		else
			echo "No Running Projects"
		fi
		
	fi
else
        echo "Invalid entry." >&2
        exit 1
fi

Using BASH command Find to locate files – Part 2

Using User-Defined Actions:

P1. Prompt the user before findings.

[u1@node1 Downloads]$ find -type f -name 'rrr' -ok ls -l '{}' ';'
< ls ... ./rrr > ? Y
-rw-r--r-- 1 u1 users 0 Nov 19 10:36 ./rrr

where

  • {} is the symbolic representation of the current path
  • ; is the required delimiter indicating the end of the command
  • -ok can replace -exec so that the user can be prompted before executing any commands.

Pre-defined Action

P1. Find the file and delete the files immediately

[u1@node1 Downloads]$ find -type f -name '*.mov' -delete

P2. Find and quit once the match has been made

[u1@node1 Downloads]$ find -type f -name '*.mov' -quit

P3. Perform the equivalent of ls -dil on the match file.

[u1@node1 Downloads]$ find -type f -name '*.mov' -ls
4554823854 70266 -rw-r--r--   1 u1   users 62574861 Sep  5  2018 ./miniconda.sh

Using Operators

[u1@node1 Downloads]$ find \(-type -f -not -name '*.txt' \) -or \( -type -r -not -name '*.doc' \)
  • Parentheses have special meaning to the shell. Putting a backslash character in front of the parenthesis help to prevent shell from interpreting them.

Options

Set the maximum number of levels that find will descend when performing tests and actions.

[u1@hpc-gekko1 Downloads]$ find -maxdepth 2 -type f -name '*' -ls
4554823854 70266 -rw-r--r--   1 u1   users 62574861 Sep  5  2018 ./miniconda.sh

Using BASH command Find to locate files – Part 1

Use command to list files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find

Count the number of files by piping the list to wc to count the files.

[u1@node1 lammps-7Aug19]$ find | wc -l
14717

Count the the directories listing by piping the list to wc to count the directories

[u1@node1 lammps-7Aug19]$ find -type d | wc -l
672

Count the list of files that matched the wildcard pattern *.mov that are larger than 1M by piping to wc to count

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -size +1M |wc -l

Find and match files or directories whose contents or attributes were last modified more recently than those of “mybasefile”

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -cnewer mybasefile

Find empty files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find -type f -empty

Find files or directories that belongs to username

[u1@node1 Downloads]$ find -type f -user root |wc -l
1572

References:

  1. The Linux Command Line

Checking Disk Usage Examples

Point 1: To check and list disk usage of /var/log directory is given below

# du -hx /var/log --max-depth=1
0 /var/log/ntpstats
57M /var/log/sa
288K /var/log/tuned
39M /var/log/audit
0 /var/log/chrony
112K /var/log/xcat
3.2M /var/log/anaconda
0 /var/log/rhsm
12K /var/log/Lenovo_Support

Point 2: Sort out and list disk usage in /var/log is given below

# du -hx /var/log --max-depth=1 |sort -h
0 /var/log/chrony
0 /var/log/ntpstats
0 /var/log/rhsm
12K /var/log/Lenovo_Support
112K /var/log/xcat
288K /var/log/tuned
3.2M /var/log/anaconda
39M /var/log/audit
57M /var/log/sa
108M /var/log

Point 3: Show last modification time and sort it by usage

# du -hx /var/log -d 1 --time -a --max-depth=1 |sort -h
0 2017-04-13 05:24 /var/log/ntpstats
0 2017-08-04 05:27 /var/log/chrony
0 2018-03-30 03:21 /var/log/tallylog
0 2018-03-30 03:27 /var/log/rhsm
0 2018-03-30 06:21 /var/log/opensm-activity.dump
0 2019-05-07 18:39 /var/log/opensm.log
0 2019-09-30 03:36 /var/log/spooler-20191006
0 2019-10-01 03:17 /var/log/btmp-20191101
0 2019-10-06 03:41 /var/log/spooler-20191013
0 2019-10-13 03:43 /var/log/maillog-20191021
0 2019-10-13 03:43 /var/log/spooler-20191021
0 2019-10-21 03:28 /var/log/maillog-20191027
0 2019-10-21 03:28 /var/log/spooler-20191027
0 2019-10-24 03:30 /var/log/boot.log-20191024
0 2019-10-25 03:30 /var/log/boot.log-20191025
0 2019-10-26 03:11 /var/log/boot.log-20191026
0 2019-10-27 03:07 /var/log/boot.log-20191027
0 2019-10-27 03:07 /var/log/spooler
0 2019-10-28 03:48 /var/log/boot.log-20191028
0 2019-10-29 03:28 /var/log/boot.log-20191029
0 2019-11-01 10:29 /var/log/boot.log
0 2019-11-01 10:29 /var/log/boot.log-20191101
0 2019-11-01 10:29 /var/log/btmp
4.0K 2018-03-30 03:21 /var/log/grubby_prune_debug
4.0K 2018-03-30 05:08 /var/log/grubby
4.0K 2019-02-25 03:06 /var/log/opensm.log-20190225.gz
4.0K 2019-02-25 19:31 /var/log/opensm.log-20190303.gz
4.0K 2019-04-07 23:25 /var/log/opensm.log-20190408.gz
4.0K 2019-05-07 17:10 /var/log/opensm.log-20190507.gz
4.0K 2019-10-14 17:30 /var/log/secure-20191021
4.0K 2019-10-24 20:06 /var/log/yum.log
4.0K 2019-11-01 15:41 /var/log/secure
8.0K 2018-09-10 15:01 /var/log/centrifydc-install.log
8.0K 2019-10-02 12:02 /var/log/secure-20191006
8.0K 2019-10-24 22:24 /var/log/secure-20191027
8.0K 2019-11-01 15:30 /var/log/maillog
12K 2018-03-30 13:41 /var/log/Lenovo_Support
.....
.....
.....
39M 2019-10-29 06:00 /var/log/audit
57M 2019-11-01 16:20 /var/log/sa
108M 2019-11-01 16:20 /var/log