Memory Loom » Bash http://192.168.0.198 Apologia, decisions, & consequences Sat, 17 May 2025 04:27:08 +0000 en-US hourly 1 http://wordpress.org/?v=3.4.2 Debian 12 PATH and X11 http://192.168.0.198/?p=592 http://192.168.0.198/?p=592#comments Sat, 18 May 2024 07:46:10 +0000 Elijah Thomas http://192.168.0.198/?p=592 A few quirks with Debain 12 include that path not being set in similar fashion to Fedora, Ubuntu, and other distributions.  One must regularly set

export PATH=$PATH:/usr/sbin

In Gnome, applications will open on the server instead of the client when using X11 forwarding.  This will occur even though xcalc opens on the client normally.  The way to avoid this is to add the following to .bashrc

unset XDG_RUNTIME_DIR 
unset GDK_BACKEND

The general rule is compress, then encrypt.1, 2

1. https://stackoverflow.com/questions/4676095/when-compressing-and-encrypting-should-i-compress-first-or-encrypt-first

2. https://crypto.stackexchange.com/questions/33737/is-it-better-to-encrypt-before-compression-or-vice-versa

]]>
http://192.168.0.198/?feed=rss2&p=592 0
Print environmental variables in Bash http://192.168.0.198/?p=1052 http://192.168.0.198/?p=1052#comments Wed, 03 Apr 2024 09:06:02 +0000 Elijah Thomas http://192.168.0.198/?p=1052 Use printenv to see what environment variables are set.

#!/usr/bin/env bash
printenv
    set -x
]]>
http://192.168.0.198/?feed=rss2&p=1052 0
The .bashrc file I use on CentOS6 http://192.168.0.198/?p=1047 http://192.168.0.198/?p=1047#comments Wed, 03 Apr 2024 09:00:37 +0000 Elijah Thomas http://192.168.0.198/?p=1047 The .bashrc file I use on CentOS6

 

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# system variables
export VISUAL=nano  # sets nano as the crontab editor
export GZIP=-9      # maximum compression
export XZ_OPT=-9    # maximum compressions

# ls modifications
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# calculator
calc() { echo "$*" | bc -l; }

# Generate a UUID
uuid() { UUID=$(cat /proc/sys/kernel/random/uuid) && echo $UUID; }

# CHANGE SHELL/WINDOW TITLE
# Works on Opensuse 15.3
# PS1=$PS1"\[\e]0;test1\a\]"
shellrename() { read -p "Enter new shell name: " name && PS1=$PS1"\[\e]0;${name}\a\]"; }

# For older versions of gnome-terminal
# shellrename() { read -p "Enter new shell name: " name && PROMPT_COMMAND='echo -ne "\033]0;${name}\007"'; }

# Zen Burn
# Another old way that works great in gnome-terminal while causing problems
# in some configurations involving SSH:
echo -ne '\e]12;#BFBFBF\a'
echo -ne '\e]10;#DCDCCC\a'
echo -ne '\e]11;#3F3F3F\a'
echo -ne '\e]4;0;#3F3F3F\a'
echo -ne '\e]4;1;#705050\a'
echo -ne '\e]4;2;#60B48A\a'
echo -ne '\e]4;3;#DFAF8F\a'
echo -ne '\e]4;4;#506070\a'
echo -ne '\e]4;5;#DC8CC3\a'
echo -ne '\e]4;6;#8CD0D3\a'
echo -ne '\e]4;7;#DCDCCC\a'
echo -ne '\e]4;8;#709080\a'
echo -ne '\e]4;9;#DCA3A3\a'
echo -ne '\e]4;10;#C3BF9F\a'
echo -ne '\e]4;11;#F0DFAF\a'
echo -ne '\e]4;12;#94BFF3\a'
echo -ne '\e]4;13;#EC93D3\a'
]]>
http://192.168.0.198/?feed=rss2&p=1047 0
The best Alias for ls http://192.168.0.198/?p=165 http://192.168.0.198/?p=165#comments Sun, 03 Mar 2024 02:14:19 +0000 Elijah Thomas http://192.168.0.198/?p=165 The best alias for ls is

ls –halF

It is easy to remember, and produces easy to read output with human readable file sizes and / marks present by the directory names..

]]>
http://192.168.0.198/?feed=rss2&p=165 0
A good version of ll http://192.168.0.198/?p=162 http://192.168.0.198/?p=162#comments Tue, 20 Feb 2024 06:37:45 +0000 Elijah Thomas http://192.168.0.198/?p=162 A really good version of the ll alias is:

ls –lahF, although on some versions ls –lahp looks better than F.

]]>
http://192.168.0.198/?feed=rss2&p=162 0
Double Sided Scan and PDF http://192.168.0.198/?p=124 http://192.168.0.198/?p=124#comments Thu, 01 Feb 2024 16:15:16 +0000 Elijah Thomas http://192.168.0.198/?p=124 The following script scans PDFs via an auto-document feeder in Hp-Lip and then collates the pages to the correct order when there are reverse sides. No scans will be saved if no data is transmitted for the second set of scans. The second set of scans is the reverse of the pages.

#!/usr/bin/env bash

YMD=$(date +"%Y-%m-%d")
Y=$(date +"%Y")
WORKDIR=/home/username/Temp/
OUTPATH=/home/username/Scans/${Y}/
#  Create a folder, ~/Temp and create a folder, ~/Scans
mkdir -p "/home/username/Scans/${Y}"

hp-scan --adf --mode=color &&
PART=hpscan
LATESTSCAN=`ls -t ${PART}*pdf | sed "1q"`
echo "${LATESTSCAN} ready for ${FILEOUTPUT}"

#  Why you shouldn't parse the output of ls
#  http://mywiki.wooledge.org/ParsingLs 
#  This does not use the FILEOUTPUT variable itself because the cp command
#  seems to have trouble copying the file correctly, even with switches. 

# Thanks Elektropepi at https://github.com/qpdf/qpdf/issues/259
function pdfscanmerge() {
	if [ $# -ne 3 ]; then 
		echo "Usage: pdfscanmerge"; return; fi let odd_pages=$(qpdf --show-npages $1) let even_page=$(qpdf --show-npages $2) argument= for (( odd_page=1; odd_page0 ]; then argument+="$2 $even_page " fi let even_page-- done qpdf --empty --pages $argument -- $3 } cp ${LATESTSCAN} "$WORKDIR$1-uncompressed-scanned-${YMD}.pdf" gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -sOutputFile="$OUTPATH${YMD}-$1.pdf" ${LATESTSCAN} sleep 1 rm -f ${LATESTSCAN} rm -f "$WORKDIR$1-uncompressed-scanned-${YMD}.pdf" killall evince read -p "Load the reverse sides and press any key:" n hp-scan --adf --mode=color && PART=hpscan LATESTSCAN=`ls -t ${PART}*pdf | sed "1q"` echo "${LATESTSCAN} ready for ${FILEOUTPUT}" cp ${LATESTSCAN} "$WORKDIR$1-uncompressed-scanned-reverse-${YMD}.pdf" gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -sOutputFile="$OUTPATH${YMD}-$1-reverse.pdf" ${LATESTSCAN} sleep 1 rm -f ${LATESTSCAN} rm -f "$WORKDIR$1-uncompressed-scanned-reverse-${YMD}.pdf" killall evince pdfscanmerge "$OUTPATH${YMD}-$1.pdf" "$OUTPATH${YMD}-$1-reverse.pdf" "$OUTPATH${YMD}-$1-collated.pdf" rm -f "$OUTPATH${YMD}-$1.pdf" rm -f "$OUTPATH${YMD}-$1-reverse.pdf"
]]>
http://192.168.0.198/?feed=rss2&p=124 0
Automated Database Restart http://192.168.0.198/?p=113 http://192.168.0.198/?p=113#comments Sun, 21 Jan 2024 22:41:11 +0000 Elijah Thomas http://192.168.0.198/?p=113 Here is a script that checks to ensure the database is running and restarts the service if it is not running.  This was used on a very low memory server to ensure a database remained operational, despite it sometimes crashing due to out of memory errors.

#!/bin/bash

timestamp="$(date)"
systemctl stop httpd && echo "${timestamp}" Stopping Apache... >> /home/centos/administration/apache.log
systemctl is-active mariadb && echo "${timestamp}" MariaDB is active so not starting >> /home/centos/administration/mariadb_active.log || echo "${timestamp}" MariaDB is not Active >> /home/centos/administration/mariadb_inactive.log
systemctl is-active mariadb && echo "${timestamp}" No need to restart MariaDB || systemctl start mariadb
systemctl start httpd && echo "${timestamp}" restarting apache...>> /home/centos/administration/apache.log
]]>
http://192.168.0.198/?feed=rss2&p=113 0
Functions in Bash http://192.168.0.198/?p=41 http://192.168.0.198/?p=41#comments Sun, 21 Jan 2024 14:57:01 +0000 Elijah Thomas http://192.168.0.198/?p=41 This is the code for a function in Bash script.

 #!/bin/bash
 # Define a function
 greet() {
 echo "Hello, $1"
 }
 # Call the function with "World" as the argument
 greet "World"
]]>
http://192.168.0.198/?feed=rss2&p=41 0
Looping through filenames in Bash http://192.168.0.198/?p=22 http://192.168.0.198/?p=22#comments Sun, 21 Jan 2024 01:59:27 +0000 Elijah Thomas http://192.168.0.198/?p=22 To loop through the subdirectories in a folder, when those subdirectories may contain spaces in the file names, use the following procedure.

OLD_IFS=$IFS && IFS=$'\n'
 for directory in $HOME/somefolder/*/; do
 echo “some code here”
 done
 IFS=$OLD_IFS

 

]]>
http://192.168.0.198/?feed=rss2&p=22 0
Bash and *nix Note no. 2 http://192.168.0.198/?p=6 http://192.168.0.198/?p=6#comments Wed, 10 Jan 2024 06:18:42 +0000 Elijah Thomas http://192.168.0.198/?p=6  

This document contains some notes on .bashrc for use with Linux systems.

alias ll=’ls -alF’
alias la=’ls -A’
alias l=’ls –CF’

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

The preceding two blocks come standard on Ubuntu as of version 2022.04. One tweak for WSL2 on Windows might be the addition of this line.

sudo bash “/etc/rc.local”

and in the rc.local follow, add the following.

#!/bin/bash
rm -f /etc/resolv.conf
echo “nameserver 1.1.1.1″ >> /etc/resolv.conf
echo “nameserver 8.8.8.8″ >> /etc/resolv.conf

This is because WSL2 may place non-routable IP addresses using 172… in resolv.conf. As of this writing, following steps recommended to disable the generation of the file fails to resolve the issue where the file is newly generated with every new terminal launched in Windows terminal. Having this run with each new Bash login ensures success with network communications.  The location /etc/rc.local may seem like an odd location for this.

The crontab to ensure to check each minute to ensure a virtual machine is running in VirtualBox is as follows.

*/1 * * * * VBoxManage startvm “VMNAME” –type headless
*/1 * * * * VBoxManage startvm “VMNAME2″ –type headless

If the virtual machine is already running, then it will not start a new copy of it. This is better than attempting to run the script via a system wide script after a reboot.  Running it this way allows a simple crontab for the user for whom the machine needs to run under. To run a script every five minutes, add the crontab as follows:

*/5 * * * * /home/USERNAME/nextcloudcron

In this example, the nextcloudcron script will run every five minutes.  This particular script is one for use contacting my Nextcloud instance for webcron.  It does not contain a .sh on the filename, because some implementations may disallow crontab scripts with file extensions.

The following checks inside a subdirectory and pulls in the files therein as bash sources. This is useful for breaking aliases, variables, and other items into different files.

if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f "$rc" ]; then
. “$rc”
fi
done
fi

The following uses nano as the crontab editor.

export VISUAL=nano

On Oracle Linux 9 on AWS, it is necessary to install Cronie to enable cron jobs.  To ensure this starts after reboots, add the following to /etc/rc.local.

bash /sbin/crond

To create a date line for a log file, use the following:

echo $(date) >> /home/USER/FILENAME

The Shebang for the top of bash files is

#!/usr/bin/env bash

To auto-update via DNF and leave a log of what was accomplished, use the following script.  The script will write a list of the updates to the systemupdates.log file, and then update the system with details of that process written to the dnfupdates.log file.

#!/usr/bin/env bash
echo $(date) >> /home/USER/systemupdates.log
sudo dnf check-update >> /home/USER/systemupdates.log
echo $(date) >> /home/USER/dnfupdates.log
sudo dnf update -y >> /home/USER/dnfupdates.log 2>&1

]]>
http://192.168.0.198/?feed=rss2&p=6 0