Skip to content

Latest commit

 

History

History
1166 lines (1062 loc) · 55.6 KB

bash.org

File metadata and controls

1166 lines (1062 loc) · 55.6 KB

Bash

Generators

Tests

interactive

if [[ $- == *i* ]]
then
    do_interactive_stuff
fi

Programms

Learning

Libraries

Tools

Fun

Completion

Frameworks

Projects

Documentation

Examples

Cheatsheet

  • Take all memory
    :(){ : $@$@;};: :
        
  • Create array and loop over values (rg is similar to grep)
    mapfile -t output < <(rg emacs "$HOME/.local/share/chezmoi"); for match in "${output[@]}"; do echo $match; done
        
    • Print array length
      ${#output[@]}
              
  • Close shell keeping all subprocess running
    disown -a && exit
        
  • Learn the difference between single and double quotes
    a=7; echo $a; echo "$a"; echo '$a'; echo "'$a'"; echo '"$a"'
        
  • extract email addresses from some file (or any other pattern)
    grep -Eio '([[:alnum:]_.-]{1,64}@[[:alnum:]_.-]{1,252}?\.[[:alpha:].]{2,6})'
        
  • Find ASCII files and extract IP addresses
    find . -type f -exec grep -Iq . {} \; -exec grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" {} /dev/null \;
        
  • command to array
    mapfile -d . -t version < /proc/sys/kernel/osrelease
        
  • export a function to SSH session
    (set -ex; foo() { echo "Hello from foo!"; }; ssh -t web37s.majordomo.ru "$(declare -f foo); export -f foo; exec bash")
        
  • meta data
#!/bin/bash
# Filename: cp-metadata

myecho=echo
src_path="$1"
dst_path="$2"

find "$src_path" |
    while read src_file; do
        dst_file="$dst_path${src_file#$src_path}"
        $myecho chmod --reference="$src_file" "$dst_file"
        $myecho chown --reference="$src_file" "$dst_file"
        $myecho touch --reference="$src_file" "$dst_file"
    done
  • one PATH per line
    echo -e ${PATH//:/\\n}
        
  • restart container based on cgroup memory
while true; do (set -ex; MEMORY_LIMIT="25769803776"; (( $(grep --max-count=1 total_rss $(grep --max-count=1 --files-with-matches $MEMORY_LIMIT /sys/fs/cgroup/memory/docker/*/memory.stat) | cut --delimiter=" " --fields=2) > $(( MEMORY_LIMIT - 536870912 )) )); docker restart mariadb-10.4.12); done
  • parse string as arguments to a +BEGIN_SRC

#functino bash (set -e; foo() { echo @ $1 $2; echo rm $1; echo ln -s $(printf $2 | sed ‘s@src/dotfiles/@.local/share/chezmoi/dotfiles/@’) $1; echo; }; eval $(find -maxdepth 5 -type l -exec stat –format=%N {} + | grep ‘dotfiles/’ | grep -v chezmoi | awk ‘{ print $1, $3 }’ | tr -d “’” | sed ‘s@^@foo @’ | tr ‘\n’ ‘;’))

  • delete single PATH from PATH variable
directory_to_remove=/d/Programme/cygwin/bin
PATH=:$PATH:
PATH=${PATH//:$directory_to_remove:/:}
PATH=${PATH#:}; PATH=${PATH%:}

# If you don't use an intermediate variable, you need to protect the / characters in the directory to remove so that they aren't treated as the end of the search text.

PATH=:$PATH:
PATH=${PATH//:\/d\/Programme\/cygwin\/bin:/:}
PATH=${PATH#:}; PATH=${PATH%:}
  • exec &> >(tee /tmp/bash.log)
  • find /gnu -type f -iname ’12x22‘|while read x;do vbr GxFont12x22- $x;done
  • Alternatives to coreutils: hexyl bat fd diskus
  • Monitor bandwidth by pid
nethogs -p eth0
  • exclude a column with cut
cut -f5 --complement
  • Press Any Key to Continue
read -sn 1 -p "Press any key to continue..."
  • Use file(1) to view device information
file -s /dev/sd*
  • List the number and type of active network connections
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
  • Runs a bash script in debugging mode
bash -x ./post_to_commandlinefu.sh
  • A child process which survives the parent’s death (for sure)
( command & )
  • Convert seconds into minutes and seconds
bc <<< 'obase=60;299'
  • dd progress
C-t
  • SVN committers
svn log -q|grep "|"|awk "{print \$3}"|sort|uniq -c|sort -nr
  • Pause Firefox
killall -STOP -m firefox
  • Prints line numbers
nl
  • Prettify XML
tidy -xml -i -m [file]
  • grep PDF file
pdftotext [file] - | grep 'YourPattern'
  • Screenshot from CLI
DISPLAY=:0.0 import -window root /tmp/shot.png
  • lshw to HTML
lshw -html > hardware.html
  • SSH IP
echo ${SSH_CLIENT%% *}
  • PDF to JPEG
for file in `ls *.pdf`; do convert -verbose -colorspace RGB -resize 800 -interlace none -density 300 -quality 80 $file `echo $file | sed 's/\.pdf$/\.jpg/'`; done
  • Copy MySQL database to new server
mysqldump --add-drop-table --extended-insert --force --log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"
  • Delete blank lines
grep . filename
  • Show ASCII key
showkey -a
  • Compare directory tries
diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
  • Backup all MySQL databases
for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
  • Kernel module dependency graph
lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | feh -
  • Which program is this port belongs to ?
lsof -i tcp:80
  • Retry the previous command until it exits successfully
until !!; do :; done
  • define quick calc procedure
? () { echo "$*" | bc -l; }
  • watch STDOUT or STDIN of proces
strace -ff -e trace=write -e write=1,2 -p SOME_PID
  • tree directory
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
  • save command output to image
ifconfig | convert label:@- ip.png
  • make window transparent in gnome shell (Set opacity via providing window id (obtained from xwininfo):)
xprop -format _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY 0x7FFFFFFF
  • top memory
ps aux | sort -rk 4,4 | head -n 10 | awk '{print $4,$11}'
  • how old filesystem is
sudo tune2fs -l $(df -h / |(read; awk '{print $1; exit}')) | grep -i created

Binary match

  <koala_man> # printf '\x00\x01\x02\xAA\xBB' | LC_ALL=C grep -ao
              $'\xAA'
      <shbot> koala_man: no output
          *** c3bolla
              (c3bolla@gateway/shell/panicbnc/x-lscmbjlppbrxdobd)
              has quit: Ping timeout: 276 seconds
  <koala_man> # printf '\x00\x01\x02\xAA\xBB' | LC_ALL=C grep -ao
              $'\xAA'; echo $?
      <shbot> koala_man:
      <shbot> koala_man: 0
  <koala_man> shbot doesn't like binary garbage output, but as
              you can see it matches
              WeeChat 2.0.1


   <guest___> cat gdb.tar.gz | grep -abo $'\00'  [20:17]
   <guest___> please try this
   <guest___> with any binary file
    <greycat> $'\00' is a nul byte which VANISHES when passed as
              an argument
   <guest___> it will print the whole file
    <greycat> THIS IS WHY YOU TELL US WHAT THE FUCK YOU ARE
              *ACTUALLY* DOING
    <greycat> You cannot pass a NUL byte as an argument, or even
              part of an argument.  Arguments are C strings.
   <guest___> greycat: i wan to search a signature in a memory
              dump
    <greycat> wooledg:~$ args $'foo\000bar'
    <greycat> 1 args: <foo>
    <Soliton> if your grep supports '\x00' it might work.
   <guest___> koala_man: if i changed \x00 to \xAA, it will warn
              illegal byte sequence
    <greycat> one of the answers on
              https://superuser.com/questions/627862/how-can-i-grep-a-hex-value-in-a-string-in-a-binary-file
              suggests grep -P '\x...'
    <greycat> of course -P is yet another GNU extension
   <guest___> greycat: my builds on Mac and Linux can't use -P
              ... :(  [20:21]
  <azizLIGHT> i suppose for my script i can just launch it again
              every hour
    <greycat> you could implement your own string search in C
              using Boyer-Moore or whatever algorithm you prefer
   <guest___> i saw this answer before
  <azizLIGHT> but id rather not
   <guest___> i tried to use hexdump/od/xdd to turn the binary to
              Hex first, but this process is very slow for large
              dump images  [20:22]
    <greycat> or you could hex-dump or octal-dump the binary file
              and then grep on the resulting ASCII strings of
              digits
          *** Vonter ([email protected]) has quit: Ping
              timeout: 256 seconds
  <koala_man> guest___: good thing you're looking for AA and not
              00 then
   <guest___> koala_man: why...?  [20:23]
  <koala_man> because yes, this is an inherent problem with nul
              bytes in arguments
          *** cjwelborn
              (cjwelborn@gateway/shell/firrre/x-edddbjnbaddspsaw)
              has quit: Ping timeout: 240 seconds
          *** c3bolla
              (c3bolla@gateway/shell/panicbnc/x-hhipangejlerxohl)
              has quit: Ping timeout: 240 seconds
  <koala_man> but you said you were looking for AA so it's fine
    <Soliton> try grep -E on osx.
          *** coolboy ([email protected]) has joined channel
              #bash  [20:24]
          *** }ls{ (~kalle@unaffiliated/ls/x-8089558) has quit:
              Ping timeout: 248 seconds
   <guest___> Soliton: thanks, i tried and it's what i said "it
              gave strange result"
   <guest___> grep -aobE "\x00"  [20:25]
   <guest___> it skipped a lot of occurences
          *** daniloaugusto
              ([email protected])
              has joined channel #bash
   <guest___> it DOES show some results but it SKIPPED most
          *** daniloaugusto
              ([email protected])
              has quit: Client Quit
    <greycat> what the fuck do you mean by "skipped"
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              joined channel #bash
    <greycat> how do you even KNOW what the output is?  did you
              pipe the output through a hex dumper?  you didn't
              show that!
          *** x405
              ([email protected])
              has quit: Ping timeout: 248 seconds
    <greycat> !wayrttd  [20:26]
    <greybot> What are you /REALLY/ trying to do!?
   <guest___> greycat: i use hexdump to view the hex values of
              the binary file and tried to search a byte appeared
              in the first 10 bytes
   <guest___> but the grep result showed me 29000 offset as the
              first result
    <greycat> 13:08  greycat> and do what, once you find it?
    <greycat> grep does not show offsets  [20:27]
          *** nkuttler (~nkuttler@unaffiliated/nkuttler) has
              joined channel #bash
   <guest___> greycat: -b
          *** navy_seal9614_ ([email protected]) has
              joined channel #bash
   <guest___> --buyte-offset
    <greycat> Jesus, fucking GNU.
    <Soliton> bsd has it as well.
    <Soliton> even has -o as well.
   <guest___> greycat: it doesn't matter, it's the same even if i
              don't use -b  [20:28]
   <guest___> i can't skip -o
          *** navy_seal9614
              ([email protected]) has
              quit: Ping timeout: 240 seconds
          *** IndianArya ([email protected]) has joined channel
              #bash
    <Soliton> so do you even need to search the whole file? or
              are you just interested in some header?
    <greycat> I think that's like the 5th attempt to get him to
              talk.  [20:29]
          *** coolboy ([email protected]) has quit: Ping
              timeout: 260 seconds
          *** lord_
              ([email protected])
              has quit: Quit: Leaving
     <geirha> as far as I know, only GNU grep is able to handle
              NULs in the data at all
   <guest___> Soliton: i want to search for a header/signature in
              the dump image  [20:30]
    <greycat> And do what, once you find it?
   <guest___> grep -c $'\x00' and grep -c "\x00" give different
              results, is it normal?
    <Soliton> yes.
    <greycat> $'\x00' is the same as ''
    <Soliton> as explained above.
    <greycat> AS YOU HAVE BEEN TOLD
    <greycat> AS YOU HAVE BEEN *SHOWN*
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              quit: Ping timeout: 276 seconds  [20:31]
          *** kallesbar ([email protected]) has quit: Ping
              timeout: 264 seconds
          *** renihs ([email protected]) has quit: Quit: bye
          *** gchristensen (~gchristen@unaffiliated/grahamc) has
              left channel #bash: "WeeChat 1.9.1"  [20:32]
          *** renihs ([email protected]) has joined channel
              #bash
          *** rcdilorenzo
              ([email protected]) has
              quit: Ping timeout: 260 seconds
          *** calamity_man ([email protected]) has joined
              channel #bash  [20:33]
          *** merzo ([email protected]) has joined channel
              #bash
          *** tunekey (~tunekey@unaffiliated/tunekey) has joined
              channel #bash
          *** fission6 ([email protected]) has quit: Quit:
              My MacBook has gone to sleep. ZZZzzz…
          *** Mista_D ([email protected]) has joined
              channel #bash  [20:34]
          *** pajpax ([email protected]) has
              joined channel #bash
   <guest___> i found that -b is buggy
   <guest___> grep -c works
    <greycat> Submit a bug report to your vendor, then.
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              joined channel #bash  [20:35]
       <djph> vendor redirects bug reports to /dev/null
       <djph> :)
          *** HarveyPwca
              ([email protected]) has
              joined channel #bash
          *** tesseract (tesseract@nat/redhat/x-exnnhbtedpbnlyrw)
              has quit: Quit:
              ♪♫•*¨*•.¸¸♫♪♪♫•*¨*•.¸¸♫♪♪♫•*¨*•.¸¸♫♪♪♫•*¨*•.¸¸♫♪
          *** migul (~mig@pdpc/supporter/student/migul) has
              joined channel #bash  [20:36]
    <phy1729> find a new vendor
    <apathor> inb4 BSD lol  [20:37]
          *** Hdphn (~akira@gateway/tor-sasl/hdphn) has quit:
              Ping timeout: 268 seconds  [20:40]
          *** x405
              ([email protected])
              has joined channel #bash
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              quit: Ping timeout: 276 seconds
   <guest___> # printf '\x00\x01\x02\xAA\xBB' | LC_ALL=C grep -ao
              $'\xAA'; echo $?  [20:41]
      <shbot> guest___:
      <shbot> guest___: 0
   <guest___> # printf '\x00\x01\x02\xAA\xBB' | LC_ALL=C grep -c
              $'\xAA'; echo $?
      <shbot> guest___: 1
      <shbot> guest___: 0
   <guest___> # printf '\x00\x01\x02\xAA\xBB\xBB\xBB' | LC_ALL=C
              grep -c $'\xBB'
      <shbot> guest___: 1
   <guest___> why doesn't it show 3 ?
   <guest___> # printf '\x00\x01\x02\xAA\xBB\xBB\xBB' | LC_ALL=C
              grep -bo $'\xBB'  [20:42]
      <shbot> guest___: Binary file (standard input) matches
          *** HarveyPwca
              ([email protected]) has
              quit: Quit: Leaving
    <greycat> you didn't tell it to treat the input as a binary
              file, for starters
   <guest___> # printf '\x00\x01\x02\xAA\xBB\xBB\xBB' | LC_ALL=C
              grep -abo $'\xBB'
      <shbot> guest___: 4:
      <shbot> guest___: 5:
      <shbot> guest___: 6:
   <guest___> # printf '\x00\x01\x02\xAA\xBB\xBB\xBB' | LC_ALL=C
              grep -ac $'\xBB'
      <shbot> guest___: 1
    <greycat> Please stop spamming the channel via the bot.
              Practice in /msg shbot, or on your local shell.
          *** obiwan90 ([email protected])
              has quit: Ping timeout: 240 seconds  [20:43]
          *** fulminator
              (~Mutter@2600:380:181f:cb45:9980:7daa:79af:3c3c)
              has joined channel #bash
          *** renihs ([email protected]) has quit: Quit: bye
          *** renihs ([email protected]) has joined channel
              #bash  [20:44]
   <guest___> i want to show you the bug
          *** fission6 ([email protected]) has joined
              channel #bash
   <guest___> greycat: koala_man:
    <greycat> Pick ONE command that you think is a bug and we
              will try to dissect that ONE command.
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              joined channel #bash
   <guest___> -abo shows 3 results, -c shows 1
          *** MagePsycho ([email protected]) has
              joined channel #bash
  <koala_man> -c counts number of lines with a match
    <greycat> -c does not treat the input as a binary file for
              starters  [20:45]
    <greycat> why didn't you include -a or other binary-input
              options when you used -c?
 <MagePsycho> $_ vs $!
          *** renihs ([email protected]) has quit: Client Quit
    <Soliton> !$_
    <greybot> "$_" expands to the last argument to the previous
              command, after expansion (man bash, Special
              Parameters)
    <Soliton> !$!
    <greybot> $! expands to the process ID of the most recently
              executed background (asynchronous) command.
          *** fulminator
              (~Mutter@2600:380:181f:cb45:9980:7daa:79af:3c3c)
              has quit: Client Quit
          *** navy_seal9614
              ([email protected]) has
              joined channel #bash  [20:49]
          *** x405
              ([email protected])
              has quit: 
          *** gtristan ([email protected]) has joined
              channel #bash
          *** peter222
              ([email protected])
              has quit: Quit: Leaving
          *** coolboy ([email protected]) has joined channel
              #bash  [20:50]
          *** IanLiu ([email protected]) has
              quit: Quit: IanLiu  [20:51]
          *** Qasker
              (Elite14787@gateway/shell/elitebnc/x-ddepecxbeolyxuzl)
              has quit: Ping timeout: 255 seconds  [20:52]
   <guest___> Why "\xAA" and $'\xAA' are different !!!??
    <greycat> The second one is interpreted by bash.
          *** navy_seal9614_ ([email protected]) has
              quit: Ping timeout: 260 seconds
          *** IanLiu ([email protected]) has
              joined channel #bash
          *** nixfreak
              (32cac5d7@gateway/web/cgi-irc/kiwiirc.com/ip.50.202.197.215)
              has quit: Quit: Ping timeout (120 seconds)
    <greycat> The first one is just the 4 literal bytes \ x A A
              and may or may not be interpreted by the program
              you send it to.
          *** predmijat
              ([email protected]) has
              quit: Quit: bye  [20:53]
          *** raz ([email protected]) has quit: Ping timeout:
              252 seconds
          *** hiya (hiya@gateway/shell/qz/x-rdsqsnjhehsgjpys) has
              quit: Ping timeout: 240 seconds
          *** giraffe
              (skarn@gateway/shell/firrre/x-jyfeyiivlyscrutn) has
              quit: Ping timeout: 240 seconds  [20:54]
          *** raz (raz@2604:180:1::6193:c4c7) has joined channel
              #bash  [20:55]
          *** raz (raz@2604:180:1::6193:c4c7) has quit: Changing
              host
          *** raz (raz@unaffiliated/raz) has joined channel #bash
          *** gef3233
              ([email protected])
              has joined channel #bash
          *** coolboy ([email protected]) has quit: Ping
              timeout: 256 seconds
          *** bl4ckr41n
              ([email protected]) has
              joined channel #bash
   <guest___> greycat: thanks! which one is better?
   <guest___> moreover, i found that \x00 is not good for testing
                                                           [20:56]
    <Soliton> the one that does what you want.
    <greycat> Which color is better?  Black or white?
   <guest___> searching \x00 doesn't work even if the whole file
              is 00 00 00 00
    <greycat> 13:07  greycat> There may not be any standard tool
              capable of it.
    <greycat> 13:21  greycat> you could implement your own string
              search in C using Boyer-Moore or whatever algorithm
              you prefer
          *** dr3w_ (~dr3w_@abercs/dr3w) has joined channel #bash
          *** docmax1 ([email protected]) has
              joined channel #bash  [20:57]
   <guest___> Soliton: greycat thanks
          *** fission6 ([email protected]) has quit: Quit:
              My MacBook has gone to sleep. ZZZzzz…
          *** predmijat
              ([email protected]) has
              joined channel #bash
          *** rememberYou (~user@unaffiliated/rememberyou) has
              joined channel #bash
          *** hph^ ([email protected]) has quit:
              Ping timeout: 260 seconds
          *** fission6 ([email protected]) has joined
              channel #bash  [20:58]
          *** IndianArya ([email protected]) has quit: Ping
              timeout: 246 seconds
     <geirha> you could hack something up using bash, but you
              risk having to store the whole file in memory if it
              doesn't contain any NUL at all  [20:59]
          *** ageis ([email protected]) has quit: Quit: exit(1);
              echo 'https://cointel.pro' > /dev/null;
              x-www-browser 'https://twitter.com/ageis'
     <geirha> another option is to parse it out of od output
          *** docmax ([email protected]) has
              quit: Ping timeout: 264 seconds
          *** docmax1 ([email protected]) is
              now known as docmax
          *** mujjingun
              (uid228218@gateway/web/irccloud.com/x-fjywizescfxgvrvc)
              has quit: Quit: Connection closed for inactivity
                                                           [21:00]
    <greycat> now we're going in circles!
     <geirha> oh, guess I should read more backlog
          *** longxia (~irc@unaffiliated/longxia) has joined
              channel #bash
          *** cjwelborn
              (cjwelborn@gateway/shell/firrre/x-dzsmqhayxagptyqa)
              has joined channel #bash  [21:01]
          *** rcdilorenzo
              (~rcdiloren@cpe-2606-A000-1118-8285-C856-5C25-944F-F935.dyn6.twc.com)
              has joined channel #bash
          *** IndianArya ([email protected]) has joined channel
              #bash  [21:02]
          *** calamity_man ([email protected]) has quit:
              Ping timeout: 256 seconds
      <Tecan> https://github.com/aizquier/typewriter-sounds
          *** leerg319 ([email protected]) has joined channel
              #bash
          *** awang_ (awang@nat/redhat/x-rzwouebcuvtjqcti) has
              quit: Ping timeout: 276 seconds
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              quit: Ping timeout: 276 seconds
          *** calamity_man ([email protected]) has joined
              channel #bash  [21:03]
          *** c3bolla
              (c3bolla@gateway/shell/panicbnc/x-jthhviazcsmtjlpp)
              has joined channel #bash  [21:04]
          *** tymczenko (~tymczenko@unaffiliated/tymczenko) has
              joined channel #bash
          *** TJ- (~root@2a02:8011:2007::5) has quit: Ping
              timeout: 245 seconds
          *** lvdombrkr ([email protected]) has joined channel
              #bash  [21:05]
          *** marduk191 (marduk191@unaffiliated/marduk191) has
              joined channel #bash
          *** arora ([email protected]) has joined channel
              #bash
          *** rendar (~rendar@unaffiliated/rendar) has quit: 
          *** Qasker
              (Elite14787@gateway/shell/elitebnc/x-hxbhhfkoocrjjvxn)
              has joined channel #bash  [21:07]
          *** ageis ([email protected]) has joined channel #bash
          *** hiya (hiya@gateway/shell/qz/x-exlobvdrohsppwyk) has
              joined channel #bash
          *** sbuj
              (~textual@2601:200:c000:dbaa:ce0:baae:3ad5:d2ad)
              has joined channel #bash
          *** TJ- (~root@2a02:8011:2007::5) has joined channel
              #bash
          *** irc50 ([email protected])
              has joined channel #bash  [21:09]
          *** merzo ([email protected]) has quit: Remote host
              closed the connection
          *** Sasazuka (~Sasazuka@unaffiliated/sasazuka) has
              joined channel #bash  [21:10]
          *** giraffe
              (skarn@gateway/shell/firrre/x-vfxmmlhzgcueqyhj) has
              joined channel #bash  [21:11]
          *** kurahaupo_ ([email protected]) has joined
              channel #bash
          *** MagePsycho ([email protected]) has
              quit: Quit: MagePsycho  [21:13]
          *** kurahaupo ([email protected]) has quit: Ping
              timeout: 276 seconds
          *** kurahaupo_ ([email protected]) has quit:
              Read error: Connection reset by peer  [21:14]
          *** kurahaupo ([email protected]) has joined
              channel #bash
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              joined channel #bash  [21:15]
          *** Alex-SF ([email protected]) has joined channel
              #bash  [21:16]
          *** awang_ (awang@nat/redhat/x-hydggudfuodjnkpi) has
              joined channel #bash  [21:17]
          *** merzo ([email protected]) has joined channel
              #bash  [21:18]
          *** yann-kaelig
              ([email protected]) has
              quit: 
          *** coolboy ([email protected]) has joined channel
              #bash  [21:19]
          *** gentunian (~seba@unaffiliated/gentunian) has quit:
              Ping timeout: 252 seconds
          *** ahmedelgabri
              (~ahmedelga@2a02:a441:9f5:1:e479:f98:60e1:3a8) has
              quit: Ping timeout: 252 seconds
          *** x1b4
              ([email protected]) has
              joined channel #bash  [21:20]
          *** Uqbar (~sandbox@unaffiliated/uqbar) has quit: Ping
              timeout: 252 seconds  [21:21]
          *** gaab ([email protected]) has quit: Remote host
              closed the connection
          *** morningdoughnut ([email protected]) has
              joined channel #bash
          *** skweek
              ([email protected]) has
              joined channel #bash
          *** krukudilo (~krukudilo@gateway/tor-sasl/kurkudilo)
              has joined channel #bash
          *** lvdombrkr ([email protected]) has quit: Quit:
              Leaving  [21:22]
          *** kurahaupo_ ([email protected]) has joined
              channel #bash
          *** Uqbar (~sandbox@unaffiliated/uqbar) has joined
              channel #bash
          *** Mista-D ([email protected]) has joined
              channel #bash
          *** sauvin_ (sauvin@about/linux/staff/sauvin) has quit:
              Remote host closed the connection
          *** bray90820
              ([email protected]) has
              quit: Read error: Connection reset by peer
          *** bray9082_
              ([email protected]) has
              joined channel #bash
          *** AfroThundr
              ([email protected])
              has joined channel #bash  [21:24]
          *** coolboy ([email protected]) has quit: Ping
              timeout: 240 seconds
          *** morningdoughnut ([email protected]) has
              quit: Client Quit
          *** stillsinn ([email protected])
              has quit: Ping timeout: 268 seconds
          *** kurahaupo_ ([email protected]) has quit:
              Read error: Connection reset by peer
          *** kurahaupo_ ([email protected]) has joined
              channel #bash  [21:25]
          *** Mista_D ([email protected]) has quit: Ping
              timeout: 264 seconds
          *** coolboy ([email protected]) has joined channel
              #bash
          *** renihs ([email protected]) has joined channel
              #bash
          *** kurahaupo ([email protected]) has quit: Ping
              timeout: 240 seconds  [21:26]
          *** JackH ([email protected]) has joined
              channel #bash
          *** CrazyH
              ([email protected]) has
              joined channel #bash
          *** jwmann ([email protected]) has quit:
              Quit: zZzZz
          *** prite ([email protected]) has quit: Ping
              timeout: 248 seconds  [21:27]
          *** gelignite ([email protected])
              has joined channel #bash  [21:28]
          *** Ignatev
              (5d5580ed@gateway/web/freenode/ip.93.85.128.237)
              has joined channel #bash  [21:30]
          *** Naib (~j@fu/hw/naib) has joined channel #bash
          *** _ng ([email protected]) has joined channel
              #bash  [21:31]
          *** grauzikas ([email protected])
              has joined channel #bash
  <grauzikas> hello
  <grauzikas> is good way to use sed -i option? some one told me
              that this isnt a good way so im asking for you
              experts :)  [21:32]
          *** tgburrin ([email protected]) has joined
              channel #bash
    <greycat> Start by describing what you actually want to DO.
  <grauzikas> i want to remove some mached lines from file :)
    <greycat> grep -v bad "$file" > tmp && mv tmp "$file"  [21:33]
   <e36freak> printf '%s\n' 'g/bad/d' w | ed -s "$file"
          *** hiya (hiya@gateway/shell/qz/x-exlobvdrohsppwyk) has
              quit: Quit: Leaving!
  <grauzikas> i see, so moving file as tmp and modify it and then
              move back to original is the best way :) ?  [21:34]
  <grauzikas> i`m wrong
    <greycat> !editing
    <greybot> In-place "editing" of files from a script is a
              really poorly thought-out idea. See
              <http://backreference.org/2011/01/29/in-place-editing-of-files/>
              and <sed-i> and <pf 13> and <faq 21>.
  <grauzikas> create tmp correct file and then move it as
              original back
          *** hiya (hiya@gateway/shell/qz/x-ivoasexfrbgrianj) has
              joined channel #bash
          *** argusbr (~online@unaffiliated/argusbr) has quit:
              Ping timeout: 256 seconds
    <greycat> The better question is "Why do you believe that you
              should write a shell script to modify this file?"
          *** OS-34584
              ([email protected])
              has joined channel #bash  [21:35]
  <grauzikas> because i need some automation and before i was
              using sed -i
          *** RickDeckard ([email protected]) has quit:
              Ping timeout: 260 seconds
    <greycat> Why do you "need some automation"?  Why is the file
              wrong in the first place?  Why can't you just fix
              it by hgand?  [21:36]
  <grauzikas> i just need to modify some services cfg files
          *** Mista-D ([email protected]) has quit: Ping
              timeout: 240 seconds
          *** argusbr (~online@unaffiliated/argusbr) has joined
              channel #bash
  <grauzikas> i cant do that by hand because i need to do same
              think for a lot of virtual servers
   <OS-34584> hello everybody
          *** erdave ([email protected]) has joined channel
              #bash
    <greycat> So you have a configuration management system in
              place, right?  Puppet or ansible or ...?
          *** ZongvaX ([email protected]) has joined channel
              #bash  [21:37]
  <grauzikas> no i`m never using automation tools like puppet or
              ansible, i`m doing everything by my hands and for
              example i need to modify VPN pptpd cfg to remove or
              replace some lines  [21:38]
    <apathor> ansible's lineinfile is nice for the situations
              where one might turn to 'sed -i'
          *** well_laid_lawn ([email protected]) has joined
              channel #bash
          *** siroko
              ([email protected])
              has quit: Ping timeout: 264 seconds
  <grauzikas> before i was using sed -i and now i`m recreating my
              bash script and want to make it as it should be :)
                                                           [21:39]
          *** Zongva ([email protected]) has quit: Ping
              timeout: 264 seconds
    <greycat> How many servers are you managing in this way?
  <grauzikas> but thanks in any way, i understand that i need to
              move all content of file to tmp file with modified
              line and then move it back to original file  [21:40]
    <greycat> that's what sed -i does behind the curtain
          *** Alex-SF ([email protected]) has quit: Quit:
              Alex-SF
          *** zro (~zro@wikimedia/zro) has joined channel #bash
                                                           [21:41]
    <phy1729> grauzikas: if you're managing "a lot" of servers by
              hand, you're doing things terribly wrong.
          *** saltystew
              ([email protected])
              has joined channel #bash  [21:42]
      <Tecan> life = whats bothering you ?
          *** saltystew
              ([email protected])
              has quit: Client Quit  [21:43]
          *** saltystew
              ([email protected])
              has joined channel #bash
          *** neo219 ([email protected]) has joined channel
              #bash
          *** The_Unknown
              (~The_Unkno@gateway/tor-sasl/theunknown/x-86092925)
              has joined channel #bash
  <grauzikas> phy1729: can you explainn then how that should be
              done in correct way ?  [21:44]
    <greycat> With some kind of configuration management system.
    <phy1729> greycat: that's my line  [21:45]
  <grauzikas> :)
          *** dr3w_ (~dr3w_@abercs/dr3w) has quit: Quit:
              https://media3.giphy.com/media/3oKIPsx2VAYAgEHC12/giphy.gif
          *** funksh0n ([email protected]) has joined
              channel #bash
          *** borkr ([email protected]) has quit:
              Quit: Leaving
   <funksh0n> Hello all.
   <funksh0n> How does one open a terminal emulator and run some
              program inside it from a bash script?  Essentially
              I want a script that will run `nvim
              /some/fixed/path` in a new window.  [21:46]
<The_Unknown> good day  [21:47]
    <greycat> xterm -e nvim /some/file
  <grauzikas> probably i cant use some cfg management tools
              because im setting up it on virtual machines and
              virtual machines templates has post-install
              scripting
          *** Sonderblade
              ([email protected]) has
              joined channel #bash
    <phy1729> You can still do config management on VMs
            * phy1729 guesses either AWS with cloud-init or
              docker
    <greycat> A server is a server.
          *** fulminator ([email protected]) has joined
              channel #bash
    <greycat> Unless of course it's docker.  Then it's just
              /ignore.  [21:48]
  <grauzikas> phy1729: you are wrong :)
          *** magyar_ (~magyar@unaffiliated/magyar) has joined
              channel #bash
   <funksh0n> Wonderful thankyou greycat
  <grauzikas> it`s ovz 7 with ez templates
  <grauzikas> :)
          *** magyar (~magyar@unaffiliated/magyar) has quit: Ping
              timeout: 240 seconds
<The_Unknown> make sure you've added permissions
          *** The_Unknown
              (~The_Unkno@gateway/tor-sasl/theunknown/x-86092925)
              has quit: Remote host closed the connection
          *** The_Unknown
              (~The_Unkno@gateway/tor-sasl/theunknown/x-86092925)
              has joined channel #bash  [21:49]
          *** Ignatev
              (5d5580ed@gateway/web/freenode/ip.93.85.128.237)
              has quit: Ping timeout: 260 seconds
          *** wandering_vagran ([email protected]) has
              quit: Remote host closed the connection
          *** calamity_man ([email protected]) has quit:
              Ping timeout: 256 seconds  [21:50]
          *** ClawOfLight ([email protected]) has joined
              channel #bash
          *** ClawOfLight ([email protected]) has left
              channel #bash: #bash  [21:51]
  <grauzikas> you mean cfg management tools like chef, pupper and
              so on or debconf if it is debian ?  [21:52]
    <greycat> NOT debconf.  The other ones.  [21:53]
  <grauzikas> can you provide an example ?  [21:54]
    <greycat> You already gave two.
          *** mckendricks ([email protected]) has quit:
              Quit: Textual IRC Client: www.textualapp.com
          *** ClawOfLight ([email protected]) has joined
              channel #bash  [21:55]
          *** dr3w_ (~dr3w_@abercs/dr3w) has joined channel #bash
     <uplime> lol pupper
          *** ClawOfLight ([email protected]) has left
              channel #bash: #bash
   <OS-34584> I have a newbie question if someone care to help. I
              thought that every bash script need to have a she
              bang at the beginning
     <uplime> no
  <grauzikas> it was an mistake :) how you can see r is near t :)
     <uplime> a shebang just tells the kernel how to execute the
              script  [21:56]
     <uplime> otherwise you can just do `bash yourscript`
    <greycat> Every script should have a shebang, yes.
     <uplime> ^ it should, but its not required
          *** clemens3 ([email protected])
              has joined channel #bash
    <greycat> If you run it as "bash yourscript" then you are not
              really executing it.  You're executing a shell and
              passing the script's filename as an argument.
          *** Strepsils ([email protected]) has quit:
              Quit: Textual IRC Client: www.textualapp.com
          *** dendazen ([email protected]) has joined
              channel #bash
    <phy1729> greycat: do you put a shebang at the top of your
              .bashrc?
   <OS-34584> thank you :)
    <greycat> Without a shebang you can't *REALLY* execute it,
              like find ... -exec myscript {} +  [21:57]
     <uplime> phy1729: how do you define script?
    <greycat> phy1729: that is not a script.  It's a dot file.
    <phy1729> bash doesn't care about that distinction when
              sourcing the file

Share terminal

Parse arguments

#!/bin/bash -e

if ! OPTS="$(getopt --options vhnbs: --long verbose,dry-run,bro,help,stack-size: --name parse-options -- "$@")"
then
    echo "Failed parsing options."
    exit 1
fi

eval set -- "$OPTS"

while true; do
    case "$1" in
        -v | --verbose )
            echo "TODO: verbose."
            ;;
        -h | --help )
            echo "TODO: Help page."
            shift
            ;;
        -n | --dry-run )
            echo "TODO: Dry-run."
            shift
            ;;
        -b | --bro )
            echo "HEY BRO!"
            shift
            ;;
        -s | --stack-size )
            echo "STACK_SIZE=\"$2\""
            shift 2
            ;;
        -- )
            shift
            break
            ;;
        * ) 
            break
            ;;
    esac
done

Infinite history

export HISTCONTROL=ignoredups
export HISTFILESIZE=-1
export HISTSIZE=-1

Prompt

case ${TERM} in
    [aEkx]term*|rxvt*|gnome*|konsole*|interix)
            PS1='\[\033]0;\u@\h:\w\007\]'
            ;;
    screen*)
            PS1='\[\033k\u@\h:\w\033\\\]'
            ;;
    *)
            unset PS1
            ;;
esac
readarray -t AVAILABLE_COLLECTION < <(${CSCLI_BIN_INSTALLED} collections list -o raw -a)
COLLECTION_TO_INSTALL=()
for collect_info in "${AVAILABLE_COLLECTION[@]:1}"; do
    collection="$(echo ${collect_info} | cut -d "," -f1)"
    description="$(echo ${collect_info} | cut -d "," -f4)"
    in_array $collection "${DETECTED_SERVICES[@]}"
#!/usr/bin/env bash

while IFS= read -r -d '' file
do
    (
        file_format="$(ffprobe -show_streams -loglevel error -print_format json "$file" | jq --raw-output '.streams[1].codec_name')"
        file_name="$(basename "$file")"
        case "$file_format" in
            opus)
                printf "%s\t%s\t%s\n" "$file_format" "$file" "$file_name"
                ;;
        esac
    )
done < <(find "${RUN_MUSIC_DIRECTORY:-/srv/music}" -type f -print0)
#!/usr/bin/env bash
new_args=()
for var in "$@"
do
    if [[ "$var" == *"--cni-conf-dir"* ]]
    then
        :
    else
        new_args+=($var)
    fi
done
echo "${new_args[@]}"