View Full Version : sh tricks


ErrorMsg
please post some usefull sh-files that do a real job
---
I start with dos2unix.sh
---
#!/bin/sh

if [ "$#" -lt "1" ]
then
echo -e "This program convert text files\nfrom dos/win to unix format"
echo "usage: `basename $0` filename "
fi

for i in $@
do
if [ -f $i ]
then
out_tmp="/tmp/`basename $i`.101.tmp"
cat $i | tr -d "\015" > "$out_tmp"
mv "$out_tmp" "$i"
fi
done
----
this shell do the same as the compiled dos2unix

alaa
mass renames extensions (or postfixes)


!/bin/bash
old=$1
new=$2
for i in *$old;
do
echo mv \"$i\" \"`basename "$i" $old`$new\"
done


running this will only print a series of mv commands, inspect them to make sure they're good then you can pipe the output to bash to run it

for instance

$ ls
foo.OGG fu.OGG
$ extrename OGG ogg
mv "foo.OGG" "foo.ogg"
mv "fu.OGG" "fu.ogg"
$ extrename OGG ogg | bash
$ ls
foo.ogg fu.ogg
$


cheers,
Alaa

alaa
I'm sure there is a better way to do this.
anyway I bind these two scripts to my multimedia-keyboard sleep and wakeup keys, the power runs xlock.

disable_xscreensaver

#!/bin/bash
sed s/"\(mode:.*\)\(random\)"/"\1off"/ ~/.xscreensaver -i
xscreensaver-command -restart


enable_xscreensaver

#!/bin/bash
sed s/"\(mode:.*\)\(off\)"/"\1random"/ ~/.xscreensaver -i
xscreensaver-command -restart


cheers,
Alaa

alaa
there is a neat tool called xclip, it allows you to manupilate the xclipboard through the command line.
I use it to make some nice bindings like:
1- open clipboard content as url of a new browser tab
2- google search for clipboard's content in new browser tab
3- freshmeat search clipboard's content in new browser tab
etc.

this neat script spellchecks the content of the clipboard then replaces it with the corrected result, this way you can effectivly add a spellchecker to any program even if it doesn't support it.

for instance if you're editing inside your browser that doesn't support spellchecking, no probs, select the text, hit the keybind run through the spellchecker, when you're done just paste back overwriting current selection with the spellchecked content.

enjoy


#!/bin/bash
TEMP=`mktemp`
xclip -o > $TEMP
xterm -fg white -bg black -e ispell $TEMP
cat $TEMP | xclip -i
rm -f TEMP


cheers,
Alaa

alaa
automatically renames mp3 files to mirror their ID3 info


#!/bin/bash
for i in "$@";
do
mp3info -p "mv \"%f\" \"%a - %t.mp3\"" "$i" | grep -v '" - .mp3"' | tr -d '?'
done


uses the same technique of echoing back the commands for you to pipe to bash.
strips some extra characters to make sane GNU/Linux friendly filenames.

cheers,
Alaa

alaa
unfortunatley aumix doesn't support muting from the command line, this script solves the problem.

I bind this to my multimedia keyboard mute key, volume up and volume down are also bound to aumix's master channel, combined with control it binds to the pcm channeland alt binds to the CD channel.


#!/bin/bash

aumix -S -f ~/.aumix.rc.a

if [ ! -f ~/.aumix.rc.b ] ;
then
aumix -v0
aumix -S -f ~/.aumix.rc.b;
fi

aumix -L -f ~/.aumix.rc.b
cp -f ~/.aumix.rc.a ~/.aumix.rc.b


cheers,
Alaa

alaa
#!/bin/bash
while : ; do
select fname in $HOME / EXIT $(ls -aF) ; do
if [ "$fname" = EXIT ] ; then
exit; fi
if [ -f "$fname" ] ; then
less "$fname"
elif [ -d "$fname" ] ; then
cd "$fname"
echo "$PWD"
break
fi
done
done


I got a bunch of other useful scripts but they rely on command line tools I hacked for myself, and the rest is PekWM specific, I'll post it if someone is interested.

cheers,
Alaa

ErrorMsg
a file splitter

Nadeen
ErrorMsg ya bro nice Topic :D

alaa, cooooooooooooooooool scripts !!!!!!!!!
I love those kind of scripts
well, not good in scripting yet, so it's a good chance to learn something from this thread :

salam

alaa
Originally posted by ErrorMsg
a file splitter

so what does it add to gnu split???

cheers,
Alaa

ErrorMsg
it's a script that use dd ,expr mainly not split
like the 1st one it's do the same as dos2unix
---
chopper.sh 1000 file
will make a 1000KB parts of the file using
dd and skip flag

ErrorMsg
my chopper is smaller than split hehe just a joke

ErrorMsg
your script alaa is not working

alaa
which script is not working??

cheers,
Alaa

ErrorMsg
#!/bin/bash
while : ; do
select fname in $HOME / EXIT $(ls -aF) ; do
if [ "$fname" = EXIT ] ; then
exit; fi
if [ -f "$fname" ] ; then
less "$fname"
elif [ -d "$fname" ] ; then
cd "$fname"
echo "$PWD"
break
fi
done
done

alaa
it works my friend, whatever problem is on your side.

so what do you mean by it doesn't work, you get some error message or do you get some borked output??

it doesn't cope well with filenames with special characters (unlike the other scripts) so maybe thats where the problem is coming from.

cheers,
Alaa

ErrorMsg
what does it do
how to quit it
---
1) something
2) something
?#
---

alaa
you quit with CTRL-C

and try to type in a number and press enter :-)

cheers,
Alaa

LI-6Y LUCY
Asalamu 3lekom plz can any one help me in this script...

ErrorMsg
it works alaa it works
---
I did not get it 1st

ErrorMsg
in function extract you use $2
here it refer to extract's second argument and extract is called without arguments
so you should
1) make global2=$2
then use it or
2) call extract $1 $2

LI-6Y LUCY
ThnX errormsg but can u expalin more can u write in a text file send to me s6lucy@imxnet.net
ThnX AloT.

ErrorMsg
it's trivialchange the line with
extract
to be
extract $1 $2
---
this is because $1 is the 1st argument not just to the script argument but also to the functions arguments!!!
$1 and $2 is the script argument in main only
---

LI-6Y LUCY
ThnX AloT Errormsg.