Saturday, February 27, 2016

bash scripting braindump

#!/bin/bash #!/bin/bash -x chmod u-x tryout.sh type test chmod a+x echo "$1$(date)>> ~/notes.txt $USER $user var3="$var1 $var2" echo $var3 x = 5 vs x=5 param="$1" datevar="$(data)" "{$foo}bar" read -p "enter here:"var
echo $var>>~/notes.txt

test expression 
[[ -e $filename ]] [[ -d $dirname ]] -n -z -h  [[ !$1 ]] [[ $userinput ]] [[ $str="user1" ]] (Spaces are important. $# script input arg count $? last command status code string compare = < > int compare -eq -ne -lt -gt ${#var} len ${#$1} help [[ if [[ ! -d $dir ]]; then if [[ $# -ne 2 ]]; then if [[ ! -d $1 ]] then
[[ $answer=[Yy] ]] [[ $answer=RegEx ]]

not expression 

if mkdir working; then if mkdir "$bindir"; then if type "$unknowncommand"; then
count_1 =$(ls -Al "dir1" | wc -l)

if type "$scriptname"; then 
exit 0
else
exit 1
fi

I/O
printf "|%20s |%20s |20%s |\n" $(ls) echo -n "no new line"
read; echo $REPLY IFS=: read a b (enter 1:2)
/dev/stdin &0 0> /dev/stdout &1 1> > /dev/stderr &2 2> /dev/null cmd_hide_error 2>/dev/null cmd_hide_output 1>/dev/null cmd_out_as_err 1>&2 cmd_out_as_err >&2 ls > logfile 2>&1 if type "$newscript" >/dev/null 2>&1; then
sum() { return $($($1+$2))} declare -i v sum 5 6 v="$?" start_with_a () { [[ $1==[aA]* ]]; if start_with_a ax;  then [[ !$1 ]]&& exit 0 error() {} >&2 isnum() { return [[ $1 =~^[0-9]+$ ]]; declare -i len="${#1}+4"
cat >>END
 function cannot return value must use v="$?" 
END
for f in "*$1"; do base=$(basename "$f" "$1") echo mv "$f ${base}$2" done mvtxt2csv txt csv touch {a..h}.txt for ((i=0;i<10 ++i)); do done
ls -l | awk '{print $5}' od -ta octal dump  ps -ax | tee acopy.txt | more
$svnV=$(svn --version --quiet) dver=$(print $svnV|sed 's/\.//g') pver=$(print $dver| cut -c1-${#pad) would be 1400

Control
while read -r; do echo $REPLY break continue done [[ $# -ne 2 ]];
case $1 in x|y|z) echo "xyz";; w) echo "" *) esac 
mkdir newDir && cd newDir mkdir existingDir || rm existingDir


declare -r const="113" declare -i p declare +i p  expression (( p=$(ls|wc -i)+10 )) declare -i t=(( ($RANDOM%10)+1 )) until(( t==3 )); do read -p $t (( t )) || continue done

declare -a ar ar=( 1 2 a b) echo ${#a[@]} ${!a[*]} count and indexes  x[0]="1" x[15]="yes" ${x[*]} ${x[@]} ${x[0]} declare -x expVar export expV2="pass into sub proc" call_subscript

substitution vs command $((..)) vs ((..)) p=$((++x)) vs ((++x))

^C+r search history
crontab -u myjobs.txt 30 00 * * * script1.sh crontab -l crontab -u jimmy -l crontab -r crontab -e
 at -f script1.sh noon tomorrow exec >log 2>errlog nohup nice longrun.sh >log & tail -f log bash -x script1 source script1 same as . script1 sleep 1

Parameter and options 
$1 ${10} $0 could be symbolic link  $@ $* $# "$@" differ "$*" for p in $@; do  script "a1 arg1" a2 a3 for p in "$@"; --"$@" positional, "@*" one arg. $@=$* no quote. do while [[ $1 ]]; do read file as 1st shift done shift 3 shift $(( OPTIND -1 ))
while getopts "b:r:s" opts; do case b) r) s) \?) script1 -r -b 5 ${OPTARG} -r stop --endofopt file.txt ":b:r:s" silence :)each "miss $OPTARG"


debugging
set -x -n -v -u -e
#!/bin/bash -x
set -x
...
set +x
$EDITOR export EDITOR=nano
#include source f1.sh = . f1.sh . .bashrc reload startup script now


No comments:

Post a Comment