Bash: Difference between revisions

From DWIKI
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
*[http://www.gnu.org/software/bash/bash.html Homepage]
=Links=
*http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html
*[http://www.gnu.org/software/bash/bash.html Homepage]  
*[http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/ Shortcuts to move faster in Bash command line]
 
*[http://en.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html Bash Guide for Beginners]
*[http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html]
*http://www.ling.helsinki.fi/users/reriksso/unix/shell.html
*[http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/ Shortcuts to move faster in Bash command line]  
*[http://en.tldp.org/LDP/abs/html/ Advanced Bash Scripting Guide]
*[http://en.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html Bash Guide for Beginners]  
*[http://www.freeos.com/guides/lsst/ Linux Shell Scripting Tutorial]
*[http://www.ling.helsinki.fi/users/reriksso/unix/shell.html http://www.ling.helsinki.fi/users/reriksso/unix/shell.html]
*[http://subsignal.org/doc/AliensBashTutorial.html Aliens Bash Tutorial]
*[http://en.tldp.org/LDP/abs/html/ Advanced Bash Scripting Guide]  
*http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/
*[http://www.freeos.com/guides/lsst/ Linux Shell Scripting Tutorial]  
*[https://ss64.com/bash/syntax-keyboard.html Bash Keyboard Shortcuts]
*[http://subsignal.org/doc/AliensBashTutorial.html Aliens Bash Tutorial]  
*[https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ Change colour of bash prompt]
*[http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/ http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/]
*[https://ss64.com/bash/syntax-keyboard.html Bash Keyboard Shortcuts]  
*[https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ Change colour of bash prompt]  
 
 
 
= Tips and tricks =
==Globbing==
===Globbing except strings===
GLOBIGNORE="*.error.log"
 
== String manipulation ==
 
=== Remove character from string ===


=Tips and tricks=
==String manipulation==
===Remove character from string===
  VERSION='2.3.3'
  VERSION='2.3.3'
  echo "${VERSION//.}"
  echo "${VERSION//.}"


===check if var contains substring===
=== Replace all characters in a string===
  [[ $foo =~ .*sub.* ]]
Replace all x,y,z with _:
"${var//+([xyz])/_}"
requires extended pattern matching
shopt -s extglob
 
=== check if var contains substring ===
 
  [[$foo_=~_.*sub.*|$foo =~ .*sub.*]]
 
=== get first character of a string ===


===get first character of a string===
  FIRST=${STRING:0:1}
  FIRST=${STRING:0:1}


==read without while loop==
 
== Date manipulation ==
===Age of a file ===
stat -C %Y $FILE
 
== read without while loop ==
 
The problem is scope, try
The problem is scope, try
  echo foo bar | { read a b;echo $b; }
  echo foo bar | { read a b;echo $b; }


==check if variable is integer==
== check if variable is integer ==
if [[ $foo =~ ^[0-9]+$ ]]


if [[$foo =~ ^[0-9]+$]]


 


 
== move cursor ==


==move cursor==
  ^b move back 1 char
  ^b move back 1 char
  ^f move forward 1 char
  ^f move forward 1 char
Line 39: Line 67:
  alf-f move forward 1 word
  alf-f move forward 1 word


==loops==
== loops ==
===loop through range of numbers===
 
=== loop through range of numbers ===
 
  for i in {0..10};do echo $i;done
  for i in {0..10};do echo $i;done


[[Category: Scripting]]
 
[[Category: Programming]]


=== exit while loop ===


===exit while loop===
  break
  break


==bash history timestamp==
 
  HISTTIMEFORMAT="%d/%m/%y %T "
 
=== skip one in loop ===
 
continue
 
== bash history timestamp ==
 
  HISTTIMEFORMAT="%y/%m/%d %T "
 
== screen and bash history ==


==screen and bash history==
To make sure all screens write to same history (at least on CentOS and RHEL):
To make sure all screens write to same history (at least on CentOS and RHEL):
  echo "history -a;history -r" > /etc/sysconfig/bash-prompt-screen
  echo "history -a;history -r" > /etc/sysconfig/bash-prompt-screen
  chmod +x /etc/sysconfig/bash-prompt-screen
  chmod +x /etc/sysconfig/bash-prompt-screen


In general:
In general:
  export PROMPT_COMMAND='history -n'
  export PROMPT_COMMAND='history -n'


==functions==
or
 
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
 
== add extensions to filenames ==
 
find . -type f |while read i;do if [[!_$i_=~_.*.txt|! $i =~ .*.txt]];then mv -v $i $i.txt;fi;done
 
 
 
== bash functions ==
 
  function foo() {
  function foo() {
     echo $1 $2
     echo "one: $1 two: $2"
   }
   }
foo "first param" 2
=== convert units ===
function convertunits() {
        C="$1"
        U="$2"
        debug "converting $C $U"
        case $U in
                "KiB") O=$((C*1000)) ;;
                "MiB") O=$((C*1000*1000)) ;;
                "GiB") O=$((C*1000*1000*1000)) ;;
                "TiB") O=$((C*1000*1000*1000*1000)) ;;
                *) O="convert error in $U" ;;
        esac
        if [ -z "$O" ];then O="convert_error for unit $U";fi
        echo "$O"
}
== Check if disk is mounted ==
mountpoint /mnt/foo
== Get local IP address ==
ip route get 1 | awk '{print $NF;exit}'
= FAQ =
== unary operator expected ==
try qouting your vars
  [[Category:Scripting]] [[Category:Programming]] [[Category:Shell]]
== failing I-search backward ==
When stuck at that, try
^g
==bash: printf: 60.29: invalid number==
Check $LC_NUMERIC
==Variable scope in while loop==
N=0
shopt -s lastpipe
foo | while read i
do
  N=$((N+1))
etc
done
echo $N

Latest revision as of 17:20, 10 December 2024

Links


Tips and tricks

Globbing

Globbing except strings

GLOBIGNORE="*.error.log"

String manipulation

Remove character from string

VERSION='2.3.3'
echo "${VERSION//.}"

Replace all characters in a string

Replace all x,y,z with _:

"${var//+([xyz])/_}"

requires extended pattern matching

shopt -s extglob

check if var contains substring

$foo =~ .*sub.*

get first character of a string

FIRST=${STRING:0:1}


Date manipulation

Age of a file

stat -C %Y $FILE

read without while loop

The problem is scope, try

echo foo bar | { read a b;echo $b; }

check if variable is integer

if [[$foo =~ ^[0-9]+$]]

 

 

move cursor

^b move back 1 char
^f move forward 1 char
alt-b move back 1 word
alf-f move forward 1 word

loops

loop through range of numbers

for i in {0..10};do echo $i;done

 

exit while loop

break

 

skip one in loop

continue

bash history timestamp

HISTTIMEFORMAT="%y/%m/%d %T "

screen and bash history

To make sure all screens write to same history (at least on CentOS and RHEL):

echo "history -a;history -r" > /etc/sysconfig/bash-prompt-screen
chmod +x /etc/sysconfig/bash-prompt-screen

In general:

export PROMPT_COMMAND='history -n'

or

export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"

add extensions to filenames

find . -type f |while read i;do if ! $i =~ .*.txt;then mv -v $i $i.txt;fi;done

 

bash functions

function foo() {
   echo "one: $1 two: $2"
 }
foo "first param" 2

convert units

function convertunits() {

       C="$1"
       U="$2"
       debug "converting $C $U"
       case $U in
               "KiB") O=$((C*1000)) ;;
               "MiB") O=$((C*1000*1000)) ;;
               "GiB") O=$((C*1000*1000*1000)) ;;
               "TiB") O=$((C*1000*1000*1000*1000)) ;;
               *) O="convert error in $U" ;;
       esac
       if [ -z "$O" ];then O="convert_error for unit $U";fi
       echo "$O"

}

Check if disk is mounted

mountpoint /mnt/foo

Get local IP address

ip route get 1 | awk '{print $NF;exit}'

FAQ

unary operator expected

try qouting your vars


failing I-search backward

When stuck at that, try

^g

bash: printf: 60.29: invalid number

Check $LC_NUMERIC


Variable scope in while loop

N=0
shopt -s lastpipe
foo | while read i
do
 N=$((N+1))
etc
done
echo $N