Shell Tricks and Silly CLI/Text Console Programs On smaller system that use 'dosbox' and 'dash' for /bin/sh some of these may not work. All should work under ksh and bash. 1. Generate a sequence of numbers or letters a. seq 1 20 b. echo {1..20} # Done in shell/set -o braceexpand c. for i in {a..m..3} d. echo {a,b,c,d}{x,y,z} # Actually, this has been around a while echo {a..d}{1..4}{x..z} 2. Suppose you want a numbered list but it needed to be in alphabetical order. This means you need smaller numbers prefixed with zeros else you get something like this: 1 1 01 2 10 02 3 11 03 4 12 04 5 13 05 6 14 06 7 15 07 8 16 08 9 17 09 10 18 10 11 19 11 12 2 12 13 20 13 14 3 14 15 4 15 16 5 16 17 6 17 18 7 18 19 8 19 20 9 20 The first column can come from for i in {1..20} do echo $i done If you want the third colum instead do this: for i in {1..20%02d} do echo $i done This will give you the same thing seq -w 1 20 3. Using 'set' to split strings. % date Sun Feb 4 02:36:58 CST 2024 % d=`date` # but this is a long string % set `date` % echo $# 6 % echo $4 02:39:42 % echo $6 2024 % set ${4//:/ } # <-- Bonus trick! // is global / is first occur. 02 39 42 % echo $2 39 % 4. Using $( ) instead of ` ` 5. Parameter/Variable expansion a. # # Remove small left pattern echo ${PWD#$HOME} b. ## # Remove large left pattern (greedy) echo ${PWD##*/} # Current directory name c. % # Remove small right pattern echo ${PWD%/*} # Gives us the full parent directory d. %% # Remove large right pattern echo ${PWD%%c/*} # in $HOME/Src/chibi-scheme e. / # Edit a="foo bar baz" # variable itself is not modified echo ${a/ bar /} echo ${HOME/rusty/pi} echo ${a//ba/--} 6. Shell Variables a. Prompts 1. PS1 Primary Prompt String !-cmd # !! for ! (variable expansion occurs) PS1='$(hostname)!!$USER --> ' Newlines can be used 2. PS2 Secondary Prompt String - used by shell when input is not complete PS2="Cont> " echo 'This is more of a test' 3. PS3 Select Command Prompt PS3="Enter # of your choice: " select p in foo bar baz quux quit do if [ "$p" == "quit" ] then break fi echo "Do something with $p" done 4. PS4 Debug Prompt PS4='$p - CMD: ' set -x Prefixes echo command in trace mode (-x) b. IFS Internal Field Separator IFS=: Used with 'in', 'for' and 'select' as 'read' read name pswd full_name home rusty::Rusty Haddock:BatCave print $name is "$full_name" and lives in $home c. RANDOM Each usage generates uniformily distributed number from 0 to 32767. Assignment to seeds the RNG unset removes special meaning. d. CDPATH : separated list of directories When argument to 'cd' does not begin with a / then the list of directories is searched for the directory argument. Current directory can be . , a null entry :: , or starting or ending : Without it, you're gonna have troubles CDPATH=$HOME:/usr/local cd bin cd Bin 7. "Here documents" sort << FOO # | tr [a-z] [A-Z] zebra alligator lemur cockatoo snake goat FOO Variables can be placed in the Here-Document and they will be evaluated even $(cmd). FOO needs to be at the beginning of the line unless... <<- Removes leading spaces CLI/Text-based Programs 1. Run a train on your terminal with 'sl' (and 'LS' as well) -a accident - people cry for help -l little train -F flying train 2. 'cmatrix' - It's not just for screensavers 3. 'figlet', 'toilet', and 'lolcat' figlet -c -f computer -w100 Hello all you miserable SCUM! | lolcat -a -F 0.5 toilet -- like figlet with additional features (unicode, color, etc) lolcat --help Add rainbow coloring 4. 'cowsay' % cowsay "Today is Pi Day" __________________ < Today is Pi Day! > ------------------ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || /usr/share/cowsay/cows -- contains other animals. Monty Python fans will notice that there is a moose file! cowsay -f moose "A human once bit my sister" Tux is in there too. 'xcowsay' puts up graphic cow under X Windows. xcowsay -f large -t 0 "I'm milking this for all I can!" 5. More animal ASCII art with 'asciiquarium'. Watch out for the sharks! Available from https://github.com/cmatsuoka/asciiquarium or CPAN 6. 'neofetch' tells you about your system. _,met$$$$$gg. rusty@krypton ,g$$$$$$$$$$$$$$$P. ------------- ,g$$P" """Y$$.". OS: Debian GNU/Linux 12 (bookworm) aarch64 ,$$P' `$$$. Host: Raspberry Pi 5 Model B Rev 1.0 ',$$P ,ggs. `$$b: Kernel: 6.1.0-rpi7-rpi-2712 `d$$' ,$P"' . $$$ Uptime: 40 days, 22 hours, 40 mins $$P d$' , $$P Packages: 1987 (dpkg) $$: $$. - ,d$$' Shell: ksh AJM 93u+m/1.0.4 2022-10-22 $$; Y$b._ _,d$P' Resolution: 1920x1080 Y$$. `.`"Y$$$$P"' DE: Cinnamon `$$b "-.__ WM: Mutter (Muffin) `Y$$ WM Theme: () `Y$$. Theme: PiXflat [GTK3] `$$b. Icons: PiXflat [GTK3] `Y$$b. Terminal: /dev/pts/0 `"Y$b._ CPU: (4) @ 2.400GHz `""" Memory: 384MiB / 8053MiB 7. 'pfetch' is a shell script that's not as verbose. Grab from GitHub - dylanaraps/pfetch -=> pfetch _____ rusty@krypton / __ \ os Debian GNU/Linux 12 (bookworm) | / | host Raspberry Pi 5 Model B Rev 1.0 | \___- kernel 6.1.0-rpi7-rpi-2712 -_ uptime 40d 22h 39m --_ pkgs 1987 memory 471M / 8053M 8. 'mg' - An EMACS-like text editor for those who can't (or don't want to) run EMACS and are not familiar with 'vi'. 9. 'sc' - A much modified version of a PD spreadsheet originally authored by James Gosling. Modified by Mark weiser (UMCP) under name of 'vc'. Further modified and renamed to 'sc' for Spreadsheet Calculator. A text-based spreadsheet program. 10. 'tac' and 'rev' 11. 'nc' or 'netcat' One of the Swiss Army knives of networking. Open TCP connections, send UDP packets, listen on arbitrary ports, port scanning, and deals with IPV4 and 6. Scripts nicer than 'telnet'. 12. 'bsdgames' Description: collection of classic textual unix games This is a collection of some of the text-based games and amusements that have been enjoyed for decades on unix systems. . It includes these programs: adventure, arithmetic, atc, backgammon, battlestar, bcd, boggle, caesar, canfield, countmail, cribbage, dab, go-fish, gomoku, hack, hangman, hunt, mille, monop, morse, number, pig, phantasia, pom, ppt, primes, quiz, random, rain, robots, rot13, sail, snake, tetris, trek, wargames, worm, worms, wump, wtf bcd - generates image of punch card -- replace ']' with ^U2337 ppt - generates image of paper tape PPT "DON'T FEED ME TO THE WORMS" rot13 trek