BreadCrumbs: Bash Scripts

Bash Scripts

From Luke Jackson

(Difference between revisions)
Jump to: navigation, search
Revision as of 21:55, 11 September 2007 (edit)
Ljackson (Talk | contribs)

← Previous diff
Current revision (07:13, 31 December 2014) (edit)
Ljackson (Talk | contribs)
(Echo Random Word X Times)
 
Line 16: Line 16:
</pre> </pre>
-== Conditional Device Check & Image Upload ==+== Bash Infinite Loop ==
 + 
 + while true; do echo Total Files: `ll | wc -l`; sleep 1; done
 + 
 +== Conditional Device Check & Image Upload (Mac OS X) ==
<pre> <pre>
#!/bin/bash #!/bin/bash
-CAMSTAT=`system_profiler SPUSBDataType | awk '{$1=$1;print}' | grep WebCam | cut -c 1-6`+CAMSTAT=`/usr/sbin/system_profiler SPUSBDataType | awk '{$1=$1;print}' | grep WebCam | cut -c 1-6`
if [ "$CAMSTAT" = "WebCam" ]; then if [ "$CAMSTAT" = "WebCam" ]; then
Line 27: Line 31:
# Create History Image # Create History Image
echo Make History echo Make History
- scp -P 2222 ~/m31uk3.jpg root@thatshawt.com:/www/thatshawt.com/htdocs/webcam/history/m31uk3_`date +%s`.jpg+ scp -P 2222 ~/m31uk3.jpg me@server.com:/www/server.com/htdocs/webcam/history/m31uk3_`date +%s`.jpg
# Update Webcam # Update Webcam
echo Take a Picture echo Take a Picture
- scp -P 2222 ~/m31uk3.jpg root@thatshawt.com:/www/thatshawt.com/htdocs/webcam/+ scp -P 2222 ~/m31uk3.jpg me@server.com:/www/server.com/htdocs/webcam/
else else
Line 38: Line 42:
</pre> </pre>
 +== Compress files into multiple archives limited by resulting archive file size ==
 +
 +<pre>
 +#!/bin/bash
 +
 +if [ $# != 3 ] ; then
 + echo -e "$0 in out max\n"
 + echo -e "\tin: input directory"
 + echo -e "\tout: output directory"
 + echo -e "\tmax: split size threshold in bytes"
 + exit
 +fi
 +
 +IN=$1 OUT=$2 MAX=$3 SEQ=0 TOT=0
 +find $IN -type f |
 +while read i ; do du -s "$i" ; done |
 +sort -n |
 +while read SIZE NAME ; do
 + if [ $TOT != 0 ] && [ $((TOT+SIZE)) -gt $MAX ] ; then
 + SEQ=$((SEQ+1)) TOT=0
 + fi
 + TOT=$((TOT+SIZE))
 + TAR=$OUT/$(printf '%08d' $SEQ).tar
 + tar vrf $TAR "$NAME"
 +done
 +
 +for i in *.tar; do gzip $i; done
 +</pre>
 +
 +== Move Files Based On Date Name ==
 +
 +<pre>
 +for ARG in $*
 +do
 + PICPATH=`echo $ARG | cut -d_ -f 2 | cut -d. -f 1 | utimecam`
 +# echo $PICPATH
 + mv -v $ARG $PICPATH/$ARG
 +# sleep 1
 +done
 +</pre>
 +
 +== Replace Multiple Strings, Words, Regular Expressions in Multiple Files from Terminal ==
 +
 +<pre>
 +find . -name '*.svg' | xargs -IX grep -E '12:(04|05|08|11|16|21|22|33|35|36|46) am 01.20.09' X | sort | wc -l
 +for i in `find . -name '*.svg' | xargs -IX grep -lE '12:(04|05|08|11|16|21|22|33|35|36|46) am 01.20.09' X` ; do
 + amcount=${#i}
 + extension="${i##*.}"
 + newname="${i%?.*}"
 + cat "$i" | sed -e 's/12:04 am 01.20.09/12:04 pm 01.20.09/' -e 's/12:05 am 01.20.09/12:05 pm 01.20.09/' -e 's/12:08 am 01.20.09/12:08 pm 01.20.09/' -e 's/12:11 am 01.20.09/12:11 pm 01.20.09/' -e 's/12:16 am 01.20.09/12:16 pm 01.20.09/' -e 's/12:21 am 01.20.09/12:21 pm 01.20.09/' -e 's/12:22 am 01.20.09/12:22 pm 01.20.09/' -e 's/12:33 am 01.20.09/12:33 pm 01.20.09/' -e 's/12:35 am 01.20.09/12:35 pm 01.20.09/' -e 's/12:36 am 01.20.09/12:36 pm 01.20.09/' -e 's/12:46 am 01.20.09/12:46 pm 01.20.09/' > "$i".fix.svg
 + echo "$i" of "$amcount"
 +diff "$i" "$i".fix.svg
 +wc -l "$i"
 +wc -l "$i".fix.svg
 +mv "$i".fix.svg "$i"
 +done
 +</pre>
 +
 +== Count Files in Subdirectories ==
 +
 + for i in `ls -1d */pixel/*trans*`; do echo -e $i = `find -E $i -regex '.*(.png|.jpg)' | wc -l`; done
 +
 +== Echo Random Word X Times ==
 +
 + for ((i=1;i<=555;i+=1)); do printf "`sed \`perl -e "print int rand(99999)"\`"q;d" /usr/share/dict/words` "; done
 +
 +== Sources ==
 +
 +* http://tldp.org/LDP/abs/html/refcards.html#AEN22664
 +* http://wiki.bash-hackers.org/syntax/expansion/brace
[[Category:Linux]] [[Category:Linux]]
[[Category:Mac OS X]] [[Category:Mac OS X]]

Current revision

Contents

Simple Wget Loop

alpha=( aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz )

alpha_count=${#alpha[*]}

x=0

while [ "$x" -lt "$alpha_count" ]
do    # List all the elements in the array.
  wget -O ${alpha[$x]}.wav http://german.about.com/library/media/sound/${alpha[$x]}.wav
  sleep 1
  let "x = $x + 1"
done

Bash Infinite Loop

while true; do echo Total Files: `ll | wc -l`; sleep 1; done

Conditional Device Check & Image Upload (Mac OS X)

#!/bin/bash

CAMSTAT=`/usr/sbin/system_profiler SPUSBDataType | awk '{$1=$1;print}' | grep WebCam | cut -c 1-6`

if [ "$CAMSTAT" = "WebCam" ]; then

 # Create History Image
 echo Make History
 scp -P 2222 ~/m31uk3.jpg me@server.com:/www/server.com/htdocs/webcam/history/m31uk3_`date +%s`.jpg

 # Update Webcam
 echo Take a Picture
 scp -P 2222 ~/m31uk3.jpg me@server.com:/www/server.com/htdocs/webcam/

else
 echo No Cam Connected!
fi

Compress files into multiple archives limited by resulting archive file size

#!/bin/bash

if [ $# != 3 ] ; then
    echo -e "$0 in out max\n"
    echo -e "\tin:  input directory"
    echo -e "\tout: output directory"
    echo -e "\tmax: split size threshold in bytes"
    exit
fi

IN=$1 OUT=$2 MAX=$3 SEQ=0 TOT=0
find $IN -type f |
while read i ; do du -s "$i" ; done |
sort -n |
while read SIZE NAME ; do
    if [ $TOT != 0 ] && [ $((TOT+SIZE)) -gt $MAX ] ; then
        SEQ=$((SEQ+1)) TOT=0
    fi
    TOT=$((TOT+SIZE))
    TAR=$OUT/$(printf '%08d' $SEQ).tar
    tar vrf $TAR "$NAME"
done

for i in *.tar; do gzip $i; done

Move Files Based On Date Name

for ARG in $*
do
  PICPATH=`echo $ARG | cut -d_ -f 2 | cut -d. -f 1 | utimecam`
#  echo $PICPATH
  mv -v $ARG $PICPATH/$ARG
#  sleep 1
done

Replace Multiple Strings, Words, Regular Expressions in Multiple Files from Terminal

find . -name '*.svg' | xargs -IX grep -E '12:(04|05|08|11|16|21|22|33|35|36|46) am 01.20.09' X | sort | wc -l
for i in `find . -name '*.svg' | xargs -IX grep -lE '12:(04|05|08|11|16|21|22|33|35|36|46) am 01.20.09' X` ; do
 amcount=${#i}
 extension="${i##*.}"
 newname="${i%?.*}"
 cat "$i" | sed -e 's/12:04 am 01.20.09/12:04 pm 01.20.09/' -e 's/12:05 am 01.20.09/12:05 pm 01.20.09/' -e 's/12:08 am 01.20.09/12:08 pm 01.20.09/' -e 's/12:11 am 01.20.09/12:11 pm 01.20.09/' -e 's/12:16 am 01.20.09/12:16 pm 01.20.09/' -e 's/12:21 am 01.20.09/12:21 pm 01.20.09/' -e 's/12:22 am 01.20.09/12:22 pm 01.20.09/' -e 's/12:33 am 01.20.09/12:33 pm 01.20.09/' -e 's/12:35 am 01.20.09/12:35 pm 01.20.09/' -e 's/12:36 am 01.20.09/12:36 pm 01.20.09/' -e 's/12:46 am 01.20.09/12:46 pm 01.20.09/' > "$i".fix.svg
 echo "$i" of "$amcount"
diff "$i" "$i".fix.svg
wc -l "$i"
wc -l "$i".fix.svg
mv "$i".fix.svg "$i"
done

Count Files in Subdirectories

for i in `ls -1d */pixel/*trans*`; do echo -e $i = `find -E $i -regex '.*(.png|.jpg)' | wc -l`; done

Echo Random Word X Times

for ((i=1;i<=555;i+=1)); do printf "`sed \`perl -e "print int rand(99999)"\`"q;d" /usr/share/dict/words` "; done

Sources

Personal tools