Well, quite self-explanatory. Only Bash and Ruby. Options “folder” and “name” are examples, replace them with what you need… Shared in hope this can help someone.
This is a really annoying problem we meet pretty often. You can meet it even if you don’t use Bash. For example, you upload an image named “my lovely cat.jpg” to your website and find no image there. Or it has no thumbnail. This may happen because you are using some program for making thumbnails that is being called from, say, PHP via backtick operator (that is, using unix shell) and that program interprets the file name passed to it as three different command line options: my, lovely and cat.jpg.
It’s not easy to remember, so here’s the reference.
var=aaa.bbb.ccc
% |
%% |
# |
## |
|
|---|---|---|---|---|
.* |
${var%.*} => aaa.bbb |
${var%%.*} => aaa |
${var#.*} => aaa.bbb.ccc (doesn’t work) |
${var##.*} => aaa.bbb.ccc (doesn’t work) |
*. |
${var%*.} => aaa.bbb.ccc (doesn’t work) |
${var%%*.} => aaa.bbb.ccc (doesn’t work) |
${var#*.} => bbb.ccc |
${var##*.} => ccc |
I like HD, it makes possible to see more details.
In Die Hard 4, in the scene where Matthew is chatting with Warlock right before McClain visit, Matthew’s desktop is visible on his Apple monitor. First interesting thing, there is Windows. Not looking like default Windows theme of course, but some icons on the desktop make it apparent (e.g. Cain and Abel – Windows password recovery tool).
Another funny thing is some kind of console there that is running.. what do you think? – of course nmap! Since The Matrix nmap is a must have utility for any serious movie about hackers (product placement, huh?). But what Matthew is running it against? Some Government or Pentagon servers? Not at all, the exact command is:
nmap -v -T4 -A insecure.org scanme.nmap.org
So he is doing his hard everyday hackers work scanning the nmap utility developers’ website and the host specially created for everybody could test nmap against it :)
I am not blaming anybody, this movie is much better in this regard than most of others. After all, what else would they scan?
I was doing a simple test of pwgen (handy utility to generate passwords) and since this test was creating a lot of files in a folder I decided to combine it with a test of ls speed vs find speed:
If you have Vim syntax highlighting enabled, the code example below will be highlighted incorrectly: 2, 3 and 4 lines (as well as the all lines below) will be all in blue as if they are comments. This happens because Vim considers everything after /* as a commentary.
1 2 3 4 |
for script in ${rpath}/*.mon.sh
do
$script ${1}
done |
Bash files test operators reference. Everybody using Bash actively should know at least -e -f and -d operators, but there are many others :)
Bash special variables reference. Everybody using Bash actively should know at least positional variables, Return value variable, Process ID variable, but there are also many others..
If you (like me) use mplayer to play music, you might want to make it possible to play songs in random order. Here is a simple script for this:
#!/bin/bash
tempfile=/tmp/randplay-$$
touch $tempfile
trap 'rm -f $tempfile; exit' 0
trap 'rm -f $tempfile; exit' 3
trap 'rm -f $tempfile; exit' 15
songsdir="${1}"
# Remove the finding playlist condition below if you want the list of files to be regenerated each time
# you run the script. This is useful if the content is changing often, but delays the script startup.
# You can also simply remove file m.playlist from the folder if its content changes.
if [ -f "${songsdir}/m.playlist" ]
then
echo "Playlist found!"
else
n=1
OFS=$IFS
IFS='
'
for song in `ls "${songsdir}"`
do
echo "$n ${song}" >> "${songsdir}/m.playlist"
n=`expr $n + 1`
done
IFS=$OFS
fi
songstotal=`cat "${songsdir}/m.playlist" | wc -l`
while true
do
echo ">>> `cat ${tempfile}|wc -l`"
sleep 1
a=$RANDOM
songsnumber=`expr $a - $a / $songstotal \* $songstotal`
[ `cat ${tempfile} | grep -c ^${songsnumber}$` -gt 0 ] && echo "Skipping `grep "^$songsnumber" "${songsdir}/m.playlist" | sed "s|^$songsnumber ||"`" && continue
echo ${songsnumber} >> ${tempfile}
songname=`grep "^$songsnumber" "${songsdir}/m.playlist" | sed "s|^$songsnumber ||"`
mplayer -quiet "${songsdir}/${songname}"
[ $? -ne 0 ] && exit 0
done
The syntax is simple:
<script name> <folder>Use ESC-ESC or PgUp to skip, Ctrl-C to stop.
There is the command eval available, that does the trick:
Home
Contacts
Downloads
RoR
Servers
Notes
Linux
ERR
Русский