# Bash ## Bash array TESTARRAY=(gowtham chaitu) echo ${TESTARRAY[1]} #### chaitu ## for loop for file in /usr/aarch64-unknown-linux-gnu/etc/portage/make.profile/* ; do echo "--->cat $file"; cat $file; done for i in $(seq 1 100); do echo $i > /dev/ttyUSB0; sleep 1; done ## switch case case $TERM in *256color* | *24Mcolor*) TERM="tmux-$TERM" ;; *) ;; esac ## create local variable set_prompt() { local curpos } export -f set_prompt; # export a function name. ## if negate if [ ! ${use_color} = '' ]; \033[6n - report cursor position ## nested condition and elif if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty1 ]] && { [ $(rc-status -r) == xwin ] || [ $(rc-stluckatus -r) == x ]; }; then rm ~/.config/autostart/Synergy-client.desktop rm ~/.config/autostart/Win11.desktop rm ~/.config/autostart/Synergy-server.desktop if [ $(rc-status -r) == x ]; then ln -s /usr/local/share/applications/Synergy-server.desktop ~/.config/autostart/Synergy-server.desktop sudo mount -o rw /dev/sda1 /mnt/IceCap elif [ $(rc-status -r) == xwin ]; then ln -s /usr/local/share/applications/Win11.desktop ~/.config/autostart/Win11.desktop ln -s /usr/local/share/applications/Synergy-client.desktop ~/.config/autostart/Synergy-client.desktop fi exec startx; fi for pack in\ $(sed -e '/^\[ebuild\s\+.\+\s\+\]\s\+\([^:]\+\):.\+\s\+.*$/!d;s//\1/'\ nfs_deps) do if ( ! ls \ /mnt/Tin/software/Linux/RPi1B2/Gentoo/musl/portage/packages/${pack}* > /dev/null 2>&1 ) || [ ! "$(find /mnt/Tin/software/Linux/RPi1B2/Gentoo/musl/portage/packages/${pack}* -mtime +4 -print)" = "" ]; then sudo emerge -1q =${pack};fi; done; ## while loop ``` i=0 while [ $i -le 31 ] || [ $i -eq 40 ]; do echo "- - -" > /sys/class/scsi_host/host${i}/scan; i=$(( $i + 1 )); done; ``` ### infinit loop ``` while true; do echo "." done ``` ### stdin to while ``` #!/bin/bash mirror() { set -x xrandr --output $2 --same-as $1 set +x } main() { count=0 mainDisp="" while IFS= read -r line; do if [ $count -eq 0 ]; then mainDisp=$line else mirror $mainDisp $line fi count=$((count+1)) done <<< $(xrandr 2>/dev/null | grep -e " connected [^(]" | \ sed -e "s/\([A-Z0-9]\+\) connected.*/\1/") } main $@ ``` ## set a variable if not set `DOCKER_REPO=${DOCKER_REPO:-"apolloauto/apollo"}` ## check if a variable is not set ``` if [ -z "$DOCKER_REPO" ];then; echo "not set";fi #or if [ ! "$DOCKER_REPO" ];then; echo "not set";fi ``` ## return from a function ``` somefunc() { if [ true ]; then return 0 fi somecomands } ``` ## block comments ``` #!/bin/bash echo before comment : <<'END' bla bla blurfl END echo after comment ``` ## test command ``` test 1 -eq 2 && echo "true" || echo "false" ``` ## Bash fork bomb ``` :(){:|:&};: ``` ## to get pid ``` someCommand& $! ``` ## to empty or delete all files in directory rm -rf * .[!.]*