I am curious about simple scripts and one liners that people use to make their life easier in Linux/Unix. Of course other shells are welcome.
Here are some examples I use.
#!/bin/bash
# name: ~/bin/backgrd
for i in `ls ~/background/` ; do xsetbg -fullscreen ~/background/$i ; sleep 5m ; done
exec ~/bin/backgrd
#done
This is my backround script I used when I use simple window managers. It is executed from the .xinitrc script (~/bin/backgrd &). It cycles thru the backgrounds I have in a folder. A new one is displayed every 5 minutes. However it doesn't work in Gnome, because it has it's own too-complicated-weirdness.
_____________
#!/bin/bash
for i in `$1`
do
$2 $3 $i $4
done
This one I don't use a whole lot, but was just made to see if I could figure out a simple way to do it. I was doing a lot of loops for dealing with a large number of files. The filename is "fin" and it is in my ~/bin file which I added to my PATH variable.
Say I downloaded a bunch of maps for quake and I need to unzip them. But I don't want to type it out 30-40 times and sit there staring at it until it gets done. So if it's in one folder all I have to do is:
fin ls unzip -o
That is equivilent of typing out: for i in "ls" ; do unzip -o $i ; done]
Pretty stupid eh?
_____________________________
#! /bin/bash
if mount /cdrom
then
echo "ok"
else
eject cdrom
fi
This is named mountlink. Every command has a return value. If it returns 0 then it worked. It will return some other value if it failed then it will return some other values, most of the time 1. This return value is stored in a variable ?. You can see what it returned by "echo $?".
If you do a "if command; then whatever; fi" that tests the return value and if it worked then it would execute what is indicated by the "then".
This command I put into a icon on my taskbar(using gnome right now). I click the icon and if the cdrom is mounted then it umounts and ejects it. If it is not mounted and there is no cd, it will open the tray for me. If there is a mountable cdrom then it mounts it, gnome detects the newly mounted file and has the icon manager put a cdrom icon on my desktop.
If a programmer tried to make a program in C to do that it would take him a long time and several thousand lines of code. And it probably be buggy anyways. This took me a couple minutes to make.
This is my favorite script and I use it the most out of any others I've made.
________________________________
#! /bin/bash
if grep 1 ~/bin/xawtv.lock
then
killall xawtv
amixer sset Line mute
echo "0" > ~/bin/xawtv.lock
else
amixer sset Line unmute
xawtv &
echo "1" > ~/bin/xawtv.lock
fi
This is along the same lines as the the mountlink script. This is named xawtvstart. My TV card is a cheaper model that uses a line in line instead of creating a new sound device so xawtv can't control the input volume. Other TV programs can do this, but xawtv is all I need, and It does everything I want when it comes to watching TV.
It uses a lock-file. Basicly if it finds a "1" in the lock file it will kill xawtv and mute line-in. Then it would put a zero in the lock file. If the lockfile only has a zero it it will unmute the line-in and start xawtv. Then put a 1 in the lock file.
I put this on a icon in my taskbar. Saves me like eight clicks with the mouse each time I want to start watching TV. 🙂
Here are some examples I use.
#!/bin/bash
# name: ~/bin/backgrd
for i in `ls ~/background/` ; do xsetbg -fullscreen ~/background/$i ; sleep 5m ; done
exec ~/bin/backgrd
#done
This is my backround script I used when I use simple window managers. It is executed from the .xinitrc script (~/bin/backgrd &). It cycles thru the backgrounds I have in a folder. A new one is displayed every 5 minutes. However it doesn't work in Gnome, because it has it's own too-complicated-weirdness.
_____________
#!/bin/bash
for i in `$1`
do
$2 $3 $i $4
done
This one I don't use a whole lot, but was just made to see if I could figure out a simple way to do it. I was doing a lot of loops for dealing with a large number of files. The filename is "fin" and it is in my ~/bin file which I added to my PATH variable.
Say I downloaded a bunch of maps for quake and I need to unzip them. But I don't want to type it out 30-40 times and sit there staring at it until it gets done. So if it's in one folder all I have to do is:
fin ls unzip -o
That is equivilent of typing out: for i in "ls" ; do unzip -o $i ; done]
Pretty stupid eh?
_____________________________
#! /bin/bash
if mount /cdrom
then
echo "ok"
else
eject cdrom
fi
This is named mountlink. Every command has a return value. If it returns 0 then it worked. It will return some other value if it failed then it will return some other values, most of the time 1. This return value is stored in a variable ?. You can see what it returned by "echo $?".
If you do a "if command; then whatever; fi" that tests the return value and if it worked then it would execute what is indicated by the "then".
This command I put into a icon on my taskbar(using gnome right now). I click the icon and if the cdrom is mounted then it umounts and ejects it. If it is not mounted and there is no cd, it will open the tray for me. If there is a mountable cdrom then it mounts it, gnome detects the newly mounted file and has the icon manager put a cdrom icon on my desktop.
If a programmer tried to make a program in C to do that it would take him a long time and several thousand lines of code. And it probably be buggy anyways. This took me a couple minutes to make.
This is my favorite script and I use it the most out of any others I've made.
________________________________
#! /bin/bash
if grep 1 ~/bin/xawtv.lock
then
killall xawtv
amixer sset Line mute
echo "0" > ~/bin/xawtv.lock
else
amixer sset Line unmute
xawtv &
echo "1" > ~/bin/xawtv.lock
fi
This is along the same lines as the the mountlink script. This is named xawtvstart. My TV card is a cheaper model that uses a line in line instead of creating a new sound device so xawtv can't control the input volume. Other TV programs can do this, but xawtv is all I need, and It does everything I want when it comes to watching TV.
It uses a lock-file. Basicly if it finds a "1" in the lock file it will kill xawtv and mute line-in. Then it would put a zero in the lock file. If the lockfile only has a zero it it will unmute the line-in and start xawtv. Then put a 1 in the lock file.
I put this on a icon in my taskbar. Saves me like eight clicks with the mouse each time I want to start watching TV. 🙂