linux:bash
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| linux:bash [2023-08-26 21:36] – created dave | linux:bash [2023-09-25 20:44] (current) – [Bash] dave | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Bash ====== | ====== Bash ====== | ||
| - | Random Linux stuff that I'm tired of looking | + | Bash stuff that I'm tired of looking |
| - | ===== HowTo ===== | ||
| - | Find the subnet given IP and mask or CIDR | ||
| - | < | ||
| - | 172.16.16.112/ | ||
| - | This assumes you have [[https:// | ||
| - | |||
| - | Pad an IP to make it sortable | ||
| - | < | ||
| - | 172.016.016.112/ | ||
| - | |||
| - | ===== Date ===== | ||
| - | My preferred date/time format is ISO 8601. | ||
| - | * < | ||
| - | Can also be accomplished with... | ||
| - | * < | ||
| - | Specific timezone and 3 hours in the future... | ||
| - | * < | ||
| ===== Command Substitution ===== | ===== Command Substitution ===== | ||
| Backticks or the other thing I never remember. | Backticks or the other thing I never remember. | ||
| - | * < | + | < |
| - | | + | < |
| ===== Loops ===== | ===== Loops ===== | ||
| - | * < | + | < |
| - | | + | < |
| ==== Ping Sweep with For Loop ==== | ==== Ping Sweep with For Loop ==== | ||
| Line 38: | Line 21: | ||
| What this does is a for loop from 1 to 254, $i takes the value of the current iteration so in the first one it will be 1 then 2, 3… and so on, then we tell it to call the ping command with the -c option which means only ping once otherwise it would ping forever after that we pipe the output to grep so we only see the hosts that actually responded and the & at the end send it to the background so it will launch all the pings in parallel. If we only want the ip address and not the whole line we can further filter this using cut. | What this does is a for loop from 1 to 254, $i takes the value of the current iteration so in the first one it will be 1 then 2, 3… and so on, then we tell it to call the ping command with the -c option which means only ping once otherwise it would ping forever after that we pipe the output to grep so we only see the hosts that actually responded and the & at the end send it to the background so it will launch all the pings in parallel. If we only want the ip address and not the whole line we can further filter this using cut. | ||
| - | ===== .inputrc ===== | ||
| - | To reload after editing... | ||
| - | bind -f ~/.inputrc | ||
| - | My usual stuff | ||
| - | # Make auto-complete cycle through options instead of listing them all | ||
| - | TAB: menu-complete | ||
| - | # Make shift tab reverse the above | ||
| - | " | ||
| - | # Use up and down arrow to search command history. Invaluable! | ||
| - | " | ||
| - | " | ||
| - | # Use Control-g to keep a command in history without executing | ||
| - | " | ||
| - | ===== find ===== | ||
| - | I never remember how to find old files... either of these will work. The second one uses minutes (I don't know why you'd want to) and shows how to exclude files. | ||
| - | find DIR -name " | ||
| - | find . ! -name ' | ||
| - | Add this to the end and anything that matches will be deleted. | + | ===== Detect if Script is Run by Cron ===== |
| - | -exec rm -f {} \; | + | # Check if script is cron or shell - two versions. |
| - | + | * [[https:// | |
| - | Limit depth to current directory. | + | |
| - | find . -maxdepth 1 -name " | + | |
| - | + | ||
| - | ===== Sort These ===== | + | |
| < | < | ||
| - | # to delete empty dirs | ||
| - | find -type d -empty -delete | ||
| - | |||
| - | # to delete files over some age & empty dirs | ||
| - | find /some/path -depth \( \( -type f -daystart -mtime +100 \) -o -type d -empty \) -ls | ||
| - | |||
| - | ======================= | ||
| - | # Check if script is cron or shell | ||
| - | https:// | ||
| if [ -t 1 ] ; then | if [ -t 1 ] ; then | ||
| echo " | echo " | ||
| Line 79: | Line 32: | ||
| #send mail | #send mail | ||
| fi | fi | ||
| + | </ | ||
| - | ----------------------- | + | < |
| CRON=$(pstree -s $$ | grep -q cron && echo true || echo false) | CRON=$(pstree -s $$ | grep -q cron && echo true || echo false) | ||
| then test with | then test with | ||
| Line 90: | Line 44: | ||
| echo "Not being run by cron" | echo "Not being run by cron" | ||
| fi | fi | ||
| - | ======================= | ||
| </ | </ | ||
linux/bash.1693085764.txt.gz · Last modified: by dave
