179 shaares
1 result
tagged
function
() {} # declare function with global scope for internal declarations
() () # declare function with local scope for internal declarations (subprocess)
DEBUG
https://linuxconfig.org/how-to-debug-bash-scripts
bash -x $script
Short | Long notation | description |
---|---|---|
set -f | set -o noglob | Disable file name generation using metacharacters (globbing) |
set -v | set -o verbose | Prints shell input lines as they are read |
set -x | set -o xtrace | Print command traces before executing command |
To see current state of all variables
echo $-
trap signals
ARG is a command to be read and executed when the shell receives the signal(s) SIGNAL_SPEC. If ARG is absent (and a single SIGNAL_SPEC is supplied) or `-', each specified signal is reset to its original value. If ARG is the null string each SIGNAL_SPEC is ignored by the shell and by the commands it invokes
trap n [condition...]
trap [action condition...]
-l # print a list of signal names and their corresponding numbers
-p # display the trap commands associated with each SIGNAL_SPEC
trap $cmd $signal # launch $cmd when signal $signal is enabled
EXAMPLES
get stdin
function _read() { while IFS= read line; do echo $line; done; }
ls | _read
give answer to command
echo -e "ct\nct"|sudo passwd root
yes ct | sudo passwd root
give the different pids of subprocess in script
file=/tmp/test
paths=$(df -l |grep "^/dev.*120.*" |sed "s|^.* \([^ ]*\)$|\1|" | sed "/save/d")
#paths='/ /var'
echo '#!/bin/bash' > $file
for path in $paths
do
echo "# $path" >> $file
echo "(echo > free2zero; pid=\$!; echo \"start $path - \\\$! \$! - \\\$BASHPID \$BASHPID - \\\$PPID \$PPID - \\\$\\\$ \$\$\"; cd $path; sleep 60; rm free2zero; echo \"end $path - \\\$! \$! - \\\$BASHPID \$BASHPID - \\\$PPID \$PPID - \\\$\\\$ \$\$\"; exit) &" >> $file
done
echo "echo \"itself : \\\$! \$! - \\\$BASHPID \$BASHPID - \\\$PPID \$PPID - \\\$\\\$ \$\$\"; sleep 30" >> $file
chmod +x $file
cat $file
sh $file
wipe free space in devices
file=/tmp/test
paths=$(df -l |grep "^/dev.*" |sed "s|^.* \([^ ]*\)$|\1|" |xargs)
paths=${paths/\/save/}
echo '#!/bin/bash' > $file
for path in $paths
do
echo "# $path" >> $file
echo "(echo \"start $path \$(date +\"%T - %N\")\"; cd $path; dd if=/dev/zero of=free2zero; rm free2zero; echo \"end $path \$(date +\"%T - %N\")\"; exit) &" >> $file
done
chmod +x $file
sh $file