NetWiki

I can make net work

User Tools

Site Tools


linux:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:bash [2023-08-26 21:46] – [Bash] davelinux:bash [2023-09-25 20:44] (current) – [Bash] dave
Line 1: Line 1:
 ====== Bash ====== ====== Bash ======
-FIXME: This isn't really bash, it's random Linux stuff that I'm tired of looking up.+Bash stuff that I'm tired of looking for.
  
-===== HowTo ===== 
-Find the subnet given IP and mask or CIDR 
-<code>ipcalc -n 172.16.16.113 255.255.255.252 | grep Network | awk '{print $2}' 
-172.16.16.112/30</code> 
-This assumes you have [[https://jodies.de/ipcalc|ipcalc]] installed (Github and download links at bottom of that page.)  FIXME: This shouldn't be under Linux. 
- 
-Pad an IP to make it sortable 
-<code>echo 172.16.16.112/30 | awk -F '[./]' '{printf "%03d.%03d.%03d.%03d/%03d\n", $1,$2,$3,$4,$5}' 
-172.016.016.112/030</code> 
- 
-===== Date ===== 
-My preferred date/time format is ISO 8601. 
-<code>date '+%Y-%m-%d_%H:%M:%S'</code> 
-Can also be accomplished with... 
-<code>date '+%F_%T'</code> 
-Specific timezone and 3 hours in the future... 
-<code>TZ="America/New_York" date -d "+3 hours" '+%Y-%m-%d_%H:%M:%S'</code> 
  
 ===== Command Substitution ===== ===== Command Substitution =====
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 
-  "\e[Z": "\e-1\C-i" 
-  # Use up and down arrow to search command history. Invaluable! 
-  "\e[A": history-search-backward 
-  "\e[B": history-search-forward 
-  # Use Control-g to keep a command in history without executing 
-  "\C-g": "\C-a history -s \C-j" 
  
-===== 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 "*_FILENAME-*.gz" -mtime +45 
-  find . ! -name '*NOT_THIS*.gz' -mmin +$((45*24*60)) 
  
-Add this to the end and anything that matches will be deleted.+===== Detect if Script is Run by Cron ===== 
 +# Check if script is cron or shell - two versions. 
 +  * [[https://stackoverflow.com/questions/3214935/can-a-bash-script-tell-if-its-being-run-via-cron|StackOverflow: Can a bash script tell if it's being run via cron?]]
 <code> <code>
--exec rm -f {} \; 
-</code> 
- 
-Limit depth to current directory. 
-<code> 
-find . -maxdepth 1 -name "v*.tar.gz" -mtime +30 
-</code>   
- 
-===== Sort These ===== 
-<code> 
-# 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://stackoverflow.com/questions/3214935/can-a-bash-script-tell-if-its-being-run-via-cron 
 if [ -t 1 ] ; then if [ -t 1 ] ; then
     echo "interacive mode";     echo "interacive mode";
Line 83: Line 32:
     #send mail     #send mail
 fi fi
 +</code>
  
------------------------+<code>
 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 94: Line 44:
     echo "Not being run by cron"     echo "Not being run by cron"
 fi fi
-======================= 
 </code> </code>
  
  
linux/bash.1693086363.txt.gz · Last modified: by dave

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki