User Tools

Site Tools


program_lang:bash

This is an old revision of the document!


BASH

backup files and folders permissions

Here an easy way to convert rwx in octal

[root@lnx01 ~]# find ./  -printf '%m:%u:%g:%p\0\n'
750:root:root:./libiconv-1.14-3.aix5.1.ppc.rpm
750:root:root:./libraqm-0.7.0-4.el7.src.rpm
755:root:root:./libssl.so.1.0.2
700:root:root:./rpmbuild
700:root:root:./rpmbuild/SOURCES
664:root:root:./rpmbuild/SOURCES/raqm-0.7.0.tar.gz
700:root:root:./rpmbuild/SPECS
644:root:root:./rpmbuild/SPECS/libraqm.spec
750:root:root:./libraqm-0.7.0-4.el7.x86_64.rpm
750:root:root:./libwebp7-1.0.3-1.el7.remi.x86_64.rpm
750:root:root:./php-mbstring-7.4.24-1.fc33.remi.x86_64.rpm
700:root:root:./php56-php-mbstring-5.6.40-28.el7.remi.x86_64.rpm

Here another more advanced output, with UID, GID…

[root@lnx01 ~]# find ./  -printf '%Y:%m:%U:%G:%a:%p\0\n'
f:750:0:0:Fri Oct 15 02:29:02.0764754025 2021:./libraqm-0.7.0-4.el7.src.rpm
f:755:0:0:Fri Oct 15 02:29:02.0789754200 2021:./libssl.so.1.0.2
d:700:0:0:Sun Oct 17 10:34:56.0564143056 2021:./rpmbuild
d:700:0:0:Sun Oct 17 23:08:59.0437891139 2021:./rpmbuild/SOURCES
f:664:0:0:Fri Oct 15 02:29:02.0837754536 2021:./rpmbuild/SOURCES/raqm-0.7.0.tar.gz
d:700:0:0:Sun Oct 17 23:08:59.0437891139 2021:./rpmbuild/SPECS
f:644:0:0:Fri Oct 15 02:29:02.0863754718 2021:./rpmbuild/SPECS/libraqm.spec
f:750:0:0:Fri Oct 15 02:29:02.0863754718 2021:./libraqm-0.7.0-4.el7.x86_64.rpm
f:750:0:0:Fri Oct 15 02:29:02.0863754718 2021:./libwebp7-1.0.3-1.el7.remi.x86_64.rpm
f:750:0:0:Fri Oct 15 02:29:02.0873754788 2021:./php-mbstring-7.4.24-1.fc33.remi.x86_64.rpm
f:700:0:0:Fri Oct 15 02:29:02.0893754927 2021:./php56-php-mbstring-5.6.40-28.el7.remi.x86_64.rpm

compare decimal

Error message when comparing 2 number, when decimal are user

integer expression expected

You can use bc Ex:

size=10.5
max_sz=10

if [[ $(bc <<<"${size} > ${max_sz}") == "1" ]]
then
  echo "${size} is bigger than ${max_sz}" 
fi

basename

Error when using basename command

[storage@lnx01l scripts]$ basename $0
basename: invalid option -- 'b'
Try 'basename --help' for more information.
[storage@lnx01l scripts]$ basename -- $0
-bash

You can also use the first occurence when using su

instead su - myuser -c "myprog.sh"
use su - myuser "myprog.sh"

Explanation: When you execute the script in the context of the running shell (which is started via the “-c” option of su), that makes it an interactive shell (hence the -bash is the value of $0). So the “basename -bash” fails as “-b” is not a valid option…

array

Define an array

allThreads=(1 2 4 8 16 32 64 128)

List all Items from array

${allThreads[@]}

List the first item

${logPaths[0]}

arr=() 	Create an empty array
arr=(1 2 3) 	Initialize array
${arr[2]} 	Retrieve third element
${arr[@]} 	Retrieve all elements
${!arr[@]} 	Retrieve array indices
${#arr[@]} 	Calculate array size
arr[0]=3 	Overwrite 1st element
arr+=(4) 	Append value(s)
str=$(ls) 	Save ls output as a string
arr=( $(ls) ) 	Save ls output as an array of files
${arr[@]:s:n} 	Retrieve n elements starting at index s

Read files

read only first line

head -1 myfile.txt

read 5 last lines

tail -5 myfile.txt

read the file without header (first line)

awk 'NR>1' myfile.txt

trap

# A control-c was trapped
ctrlc()
{
  log "Control-C trapped"
  quit -1
}

# Trap the control-c
trap ctrlc INT
program_lang/bash.1659690694.txt.gz · Last modified: 2022/08/05 11:11 by manu