Category Archives: bash

simple progress bar in bash script

Example of a simple progress bar in bash script:

#!/bin/bash

PAGELOADS=30000;
PBAR_LEN=30;
PBAR_STEP=$[$PAGELOADS/$PBAR_LEN]

for i in `seq 1 $PAGELOADS`
do
fname="`date +%F-%T`.html"
time wget http://site/page -O $fname >> .log 2>>.log
cat $fname | grep NATURE > /dev/null && rm $fname
sleep 1

#simple progress bar for fun:
clear;
PBAR=$[$i/$PBAR_STEP]
echo -n "[";
for j in `seq 1 $PBAR_LEN`
do
if [ $j -gt $PBAR ]; then
echo -n ".";
else
echo -n "*";
fi
done
echo "] [$i/$PAGELOADS]";

done

[bash] ping a host until it responds

example:

# for i in `cat ~/mysql-list.txt`; do ssh -t root@${i} "hostname; date; echo \"FLUSH TABLES;\" | mysql; shutdown -fr now && date; sleep 40"; sleep 20; while true; do ping -c1 -W2 ${i} 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then ping -c1 ${i}; break; fi; done; done

# for i in `seq -w 1 459`; do ssh root@srv0${i} "hostname; date; shutdown -fr now && date; sleep 40"; sleep 20; while true; do ping -c1 -W2 srv0${i} 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then ping -c1 srv0${i}; break; fi; done; done

——

bash simple progress counter example
p=0; t=0; for USER in `ls /home/www`; do t=$(($t+1)); test "current_user" = "${USER}" && p=$t; done; echo $p "/" $t

bash loop

line-based list expansion:

(IFS=$'\n';for i in $(cat /tmp/foo);do
echo $i
done)

I’m always forgetting the syntax to make “for” loops in Bash. I guess it serves me right for using foreach most of my UNIX life instead. Anyhow, I know I will have to come back here to find it, so I thought I would write put up this quick example with the hope that it will be useful to others as well.

for i in $(seq 1 100); do echo -n “file${i} “; touch file${i} 2>&1; done

The the above for loop will c
reate 100 files (called file1, file2, etc.).

http://spiralbound.net/2007/07/23/bash-for-loop-example

Handling Filenames with Spaces in Bash

http://www.macgeekery.com/tips/cli/handling_filenames_with_spaces_in_bash

If you set IFS to $’n’ then it will only split on newlines, not spaces. And you can enclose the entire thing in a subshell using ( foo ) to scope IFS to just that for loop.

Example:

(
IFS=$’n’
for file in $files
do
echo “$file” rocks.
done
)

# bash reminder

// частеноко забываю..:(

# (some) HISTORY SEARCH AND REPLACE:

# execute the most recent command the contains the following string:
!?string

# globally search replace on the previous line:
!!:gs/old/new/

# Keyboard shortcuts:
CTRL-k: delete (‘kill’) from cursor position to the end of the line
* CTRL-u: delete from cursor position to the beginning of the line
* ALT-d: delete from cursor position to the end of the current ‘word’
* CTRL-w: delete from cursor position to the beginning of the current ‘word’
* CTRL-a: move cursor to the first character of the line
* CTRL-e: move cursor beyond the last character of the line
* ALT-a: move cursor to the first character of the current ‘word’
* ALT-e: move cursor to the last character of the current ‘word’
* CTRL-y: insert latest deleted ‘word’
* ESC-_ or !$: repeats the last argument of the previous command.