I was messing around with some lurking [extremely quiet] geniuses on IRC and said "How can I assign a whole bunch of keystrokes to one keystroke? So maybe I could hit Windows [super key] and it would print out "#!/usr/bin/perl -w" for me, so I don;t have to type it each time."
Well, I got no answer, as usual. So then I just thought up my own way, I could edit the way I call nano.
In my real life example, I used an "alias" in my ~/.bashrc file since I didn't have root access to the box I was using. But I will show you two ways here:
1. create a file "pnano" and in it type:
#!/bin/bash echo "#!/usr/bin/perl -w" > $1 nano $1 CT=`cat $1 | wc -l` if [ $CT == 1 ]; then rm $1 fi
2. chmod it so it's executable:
chmod +x pnano
3. put it into your $PATH, I usually put things into bin:
sudo cp pnano /usr/bin/
This will create a new "executable" program. Let's look at the code in the script above, line by line. This tyoe of scripting is called "shell scripting" Line 1. shows the shell what to use, as the interpreter, to "run," or "execute" the application. In our case this will be bash. Line 2. put the line "#!/usr/bin/perl -w" into the file that you gave as an argument "$1". This means that if you do:
pnano lolhi.pl
This will put the shebang line right into the file "lolhi.pl" bringing us to our next line. Line 3. runs nano and opens the file [from your argument] $1, or as in our example "lolhi.pl"
Once opened, you will see the shebang line in the file, and you can begin coding.
You may want to add a few arguments to nano that are extremely useful:
-w means "no wrap" meaning when you hit the end of your terminal screen with a long line, it won't automatically hit RETURN for you giving you a new line. I think that should be default and has made me hate nano in the past. Next you should add to line 3:
nano +2 $1
The "+2" will put you at line two so you don't have to hit down before coding. Line 4. puts the value evaluated by "cat FILENAME | wc -l" which means "how many lines are in the file you just opened with pnano" into the variable $CT. We do this so that if you close nano without typing any code, you aren't left with a file named $1 [whatever you put after pnano] with one line it it: "#!/usr/bin/perl -w"
so there's an if statement: if it's "empty" [only shebang line exists] rm it. Then "fi" ends the if block. That's a fun exercise.
If you do not have root access to the box you code in you can make an alias statement in your ~/.bashrc file like so:
alias pnano='/home/USERNAME/pnano'
And put that anywhere in your ~/.bashrc file.
Another cool tip for those who use nano to write their Perl applications is the /etc/nanorc file. This was recently pointed out to me by Tully. What it does is automatically color your code so you can read it easier. To use it, simply uncomment the line that looks like so:
# include "/usr/share/nano/perl.nanorc"Just take away the "#" and save it. next time you open a Perl .pl file, it will be in color. <3 EDIT: I have already clobbered a file with the ">" in the echo system call :-( So what I did was use the
if [ -f $1 ]; thensyntax to check to see if the file already exists or not before clobbering it! check it out:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
nano $FILE
else
echo "#!/usr/bin/perl -w" > $1
nano +2 $1
CT=`cat $1 | wc -l`
if [ $CT == 1 ]; then
rm $1
fi
fi
Enjoy ;)
WeakNet Labs is a computer lab I created to learn more about computer science. Security became the number 1 topic over time and programming seems to have taken it's place since. Writing security themed programs, WeakNet Linux and such, has been not only great experience for us, but it helped a few of us land good solid career-like jobs.




0 comments:
Post a Comment