Author Topic: Random Shellscript Snippits  (Read 9074 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Random Shellscript Snippits
« on: January 31, 2008, 11:15:56 pm »
This has probably been done before, but whatever. People sometimes ask me for snippits of shellscript to do something handy. I figured I'd start posting them somewhere for the benefit of others. Feel free to contribute, but please stay on topic. If you want to discuss one, please start a new thread. (discussion here)

Find all files in the directory with the same contents as a specified file
$ md5sum * | grep `md5sum filename | cut -b1-32`

(bash) Print all duplicate files in a folder
for i in `md5sum * | cut -d\   -f1 | sort | uniq -d`; do md5sum * | grep $i; done


<edit> (on both my and tmp's posts) -- changed title to shellscript instead of shellcode. Oops! :)
« Last Edit: February 02, 2008, 01:52:16 pm by iago »

Offline mynameistmp

  • Full Member
  • ***
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Random Shellscript Snippits
« Reply #1 on: February 02, 2008, 02:01:19 am »
perl can be a powerful stream editor. Most of you are probably familiar with perl command line execution a la the  -e flag.  Combine it with -p and -i and you get a worthy sed alternative! The -p flag tells perl to iterate the command given by -e over every command line arg (in this case, a filename, hopefully). If there are no command line args, it'll read from STDIN. The -i flag writes the output of the -p iteration back to the argument (writing the file).

Examples:

tmp@tmp:~$ perl -e 'print "test"x10;'

-Print 'test' to STDOUT 10 times. For those of you who are behind on the times ;P

tmp@tmp:~$ perl -pe 's/^(\s+)?(telnet|shell|login|exec)/# $2/' /etc/inetd.conf

-Comment out any uncommented (running) instances of telnet, shell, login, exec in inetd.conf. Note: this will only print to STDOUT, it will not edit the file.

tmp@tmp:~$ perl -pi -e 's/bgcolor=#ffffff/bgcolor=#000000/i' *.html

-Change the background colour on any html page in the directory from black to white. The i after the substitution regex is needed to negate case sensitivity. WARNING: The previous command does WRITE to all of the files specified as command line args.

Most of you can probably infer the empowering implications of this little morsel of scripting prestidigitation.

« Last Edit: February 02, 2008, 01:51:50 pm by iago »

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Random Shellscript Snippits
« Reply #2 on: February 02, 2008, 04:45:15 pm »
Woops, I should have posted this here originally.

Quote
andrew@roflbirds:~$ perl -pi -e 'tr/a-mn-zA-MN-Z/n-za-mN-ZA-M/;'
newbys mom
arjolf zbz
has got it goin on
unf tbg vg tbva ba

Offline nslay

  • Hero Member
  • *****
  • Posts: 786
  • Giraffe meat, mmm
    • View Profile
Re: Random Shellscript Snippits
« Reply #3 on: December 25, 2010, 01:23:48 pm »
perl can be a powerful stream editor. Most of you are probably familiar with perl command line execution a la the  -e flag.  Combine it with -p and -i and you get a worthy sed alternative! The -p flag tells perl to iterate the command given by -e over every command line arg (in this case, a filename, hopefully). If there are no command line args, it'll read from STDIN. The -i flag writes the output of the -p iteration back to the argument (writing the file).

Examples:

tmp@tmp:~$ perl -e 'print "test"x10;'

-Print 'test' to STDOUT 10 times. For those of you who are behind on the times ;P

tmp@tmp:~$ perl -pe 's/^(\s+)?(telnet|shell|login|exec)/# $2/' /etc/inetd.conf

-Comment out any uncommented (running) instances of telnet, shell, login, exec in inetd.conf. Note: this will only print to STDOUT, it will not edit the file.

tmp@tmp:~$ perl -pi -e 's/bgcolor=#ffffff/bgcolor=#000000/i' *.html

-Change the background colour on any html page in the directory from black to white. The i after the substitution regex is needed to negate case sensitivity. WARNING: The previous command does WRITE to all of the files specified as command line args.

Most of you can probably infer the empowering implications of this little morsel of scripting prestidigitation.



Why are you using perl this way? You yourself know about sed and that's universally available on every Unix-like system.
An adorable giant isopod!

Offline nslay

  • Hero Member
  • *****
  • Posts: 786
  • Giraffe meat, mmm
    • View Profile
Re: Random Shellscript Snippits
« Reply #4 on: December 25, 2010, 01:25:32 pm »
Woops, I should have posted this here originally.

Quote
andrew@roflbirds:~$ perl -pi -e 'tr/a-mn-zA-MN-Z/n-za-mN-ZA-M/;'
newbys mom
arjolf zbz
has got it goin on
unf tbg vg tbva ba

tr? Why perl? You're totally killing bunnies with rocket launchers!
An adorable giant isopod!

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Random Shellscript Snippits
« Reply #5 on: December 27, 2010, 02:57:29 pm »
Woops, I should have posted this here originally.

Quote
andrew@roflbirds:~$ perl -pi -e 'tr/a-mn-zA-MN-Z/n-za-mN-ZA-M/;'
newbys mom
arjolf zbz
has got it goin on
unf tbg vg tbva ba

Nice to know it exists outside perl. I haven't used perl in so long I forget all its syntax.

Awk is my current tool of choice for text-processing. What do you think of it? How does it compare to sed? (Never used sed, only awk.)
« Last Edit: December 27, 2010, 03:10:29 pm by Ender »

Offline nslay

  • Hero Member
  • *****
  • Posts: 786
  • Giraffe meat, mmm
    • View Profile
Re: Random Shellscript Snippits
« Reply #6 on: December 28, 2010, 12:32:30 am »
Awk is also my favorite. I'm not savvy with sed beyond the basic substitution command.
An adorable giant isopod!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Random Shellscript Snippits
« Reply #7 on: December 28, 2010, 01:09:07 am »
I love awk and perl.  Very handy stuff!

Offline nslay

  • Hero Member
  • *****
  • Posts: 786
  • Giraffe meat, mmm
    • View Profile
Re: Random Shellscript Snippits
« Reply #8 on: February 07, 2011, 04:11:22 pm »
Here's a script that will capture output from a selected X11 window and store a sequence of images. It uses ffmpeg to do so. There is also a recordmydesktop project. However, I wanted a sequence of images, not a video file.

Example command:
Code: [Select]
./recordwindow.sh -r 24 "window-%03d.png"

Now select the window you want to record. Do not move the window or move other windows on top of said window. When finished, press 'q' on the prompt where you ran ./recordwindow.sh. It will not record the window decorations.  You'll need to hack the script if you want the window decorations.

I made this script to use with the animate package in LaTeX.

Code: [Select]
#!/bin/sh

framerate=10

args=`getopt 'r:' $*`
set -- $args
while true
do
        opt=$1

        shift

        case "${opt}"
        in
                -r)
                        framerate=$1
                        shift
                ;;
                --)
                        break
                ;;
        esac
done

if [ $# -ne 1 ]
then
        echo "usage: $0 [-r rate] outfile" 2>&1
        exit 1
fi

outfile=$1

flags=`xwininfo | awk '/Absolute upper-left X:/ { x = $4 }
/Absolute upper-left Y:/ { y = $4 }
/Width:/ { w = $2 }
/Height:/ { h = $2 }
END {
        print "-f image2 -s " w "x" h " \"'"${outfile}"'\" -f x11grab -s " w "x" h " -i '"${DISPLAY}"'+" x "," y
}'`

eval ffmpeg -r ${framerate} ${flags}
An adorable giant isopod!