google.com/search?q=Bash+shell
- Arrays
- Filenames
- dirname, basename
- https://www.google.com/search?q=basename+without+extension
- https://www.cyberciti.biz/faq/bash-get-basename-of-filename-or-directory-name/
- echo “${FILE##*.}“
- Files
- https://www.google.com/search?q=cat+file+while+read
- https://stackoverflow.com/questions/5626374/while-read-line-do-and-grep-problems
- http://www.compciv.org/topics/bash/loops/
- https://www.google.com/search?q=cat+file+while+read+scope
- https://stackoverflow.com/questions/16854280/a-variable-modified-inside-a-while-loop-is-not-remembered
- Numbers
- https://stackoverflow.com/questions/8385627/subtract-two-variables-in-bash
count=$((FIRSTV-SECONDV))
– fails if not pure numbers, any preceding zeros e.g. (0123)count=$(expr $FIRSTV - $SECONDV)
– a safer solution, though DO cost more (execution)
- https://stackoverflow.com/questions/8385627/subtract-two-variables-in-bash
- Text
- Trim text
https://stackoverflow.com/questions/16842014/redirect-all-output-to-file-using-bash-on-linux
https://www.google.com/search?q=color+coded+text+output+from+command+line
https://misc.flogisoft.com/bash/tip_colors_and_formatting
https://www.google.com/search?q=cli+capture+colored+text+to+file
https://askubuntu.com/questions/502445/saving-output-of-a-grep-into-a-file-with-colors
https://www.google.com/search?q=bash+script+capture+all+outputs+to+file
tee command – https://www.google.com/search?q=tee+command
Contents
Loops
Handle Arguments – Files w/ Possibly Spaces
Like quite common on macOS.
# ... regular options handling, getopt... # after such loop, now the remainder of arguments, assume files/directories COUNTER=0 while [ "$*" != "" ] ; do FILE_OR_PATH=$1 shift 1 if [ -e "$FILE_OR_PATH" ]; then COUNTER=$((++COUNTER)) fi echo "$COUNTER $FILE_OR_PATH" # process_one_file "$FILE_OR_PATH" done
PATH, including Spaces
See also .bash_profile, .bashrc, … for a bit more details on how different files are used.
$HOME/.bash_profile – File
MBP17:~ $ cat ~/.bash_profile if [ -f $HOME/.bashrc ]; then source $HOME/.bashrc fi
$HOME/.bashrc – File
MBP17:~ $ cat ~/.bashrc alias la='ls -a' alias ll='ls -l' alias vlc='/Applications/VLC.app/Contents/MacOS/VLC' alias cvlc='/Applications/VLC.app/Contents/MacOS/VLC -I rc' # general path munging PATH=${PATH}:~/bin PATH=${PATH}:/usr/local/bin F=~/.dropbox/comgt_init.sh if [ -e $F ] ; then . $F fi MBP17:~ $
$HOME/.dropbox/comgt_init.sh – File
See also our Dropbox section.
MBP17:~ $ cat ~/.dropbox/comgt_init.sh # 2020-12-07 # :::bash DROPBOX_PERSONAL=$(python -c "import json;f=open('$HOME/.dropbox/info.json', 'r').read();data=json.loads(f);print data.get('personal', {}).get('path', '')") DROPBOX_WORK=$(python -c "import json;f=open('$HOME/.dropbox/info.json', 'r').read();data=json.loads(f);print data.get('business', {}).get('path', '')") # DROPBOX_PERSONAL=$(echo $DROPBOX_PERSONAL|tr -d '\n') # DROPBOX=0 if [ "$DROPBOX_PERSONAL" != "" ] ; then DROPBOX=$DROPBOX_PERSONAL else DROPBOX="$DROPBOX_WORK" fi alias dbt='set | grep DROPB ' PATH="$PATH:$DROPBOX/Office/bin" MBP17:~ $
Use
Last login: Sat Feb 6 04:49:15 on ttys093 MBP17:~ $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/goham/bin:/usr/local/bin:/goham/johan/Dropbox (Holy Moly Limited)/Office/bin MBP17:~ $ which mlabstranscode.sh /Users/goham/Dropbox (Holy Moly Limited)/Office/bin/mlabstranscode.sh MBP17:~ goham$
eop