Jump to content
Science Forums

Recommended Posts

Posted

I'm back, baby, and I like making returns with a bang! For those of you who don't follow this section, i haven't been around for a couple of months, taking a break from the daily Comp-Sci posting. So what a better way to get back then with a bangin' post about "bang", which is not what you were probably thinking when you were covered by cold sweat clicking link to check out this thread; but you will need a cold shower once I'm done here... All in due time :)

 

Let me note here when i start a quote with a #, that means it's a command, everything but the quotes around it and # is a part of that command, so "# df -h" means that i am executing a command: df -h

 

So what is a "bang"? Those of you who are Nix people may have heard the term, the old school guys will remember it, but "bang" is simply an exclamation mark "!", and "Bangin'" is all about using it to make your command line experience lazier, more fun and more efficient. It's called and referred to as bang by the Nixians.

 

Now that you know what a "bang" is, lets talk about what it does. Bang is generally used to call up commands from history. In Bash you can use "# history" to view all the command lines in that are in bash history for that user. Note that those commands are numbered, and yes we can recall those individual commands at our leisure convenience. But lets talk about that later.

 

So, why and when are the most useful times to execute commands from history? Well, here are the ones i see most often:

 

* To repeat the last command, use "# !!"

 

"# ifconfig eth0" - this would give you the configuration of your first ethernet interface

"# !!" - and this would execute "# ifconfig eth0"

 

more useful example:

"# cat /var/log/messages"

"# !! | less" - executes "cat /var/log/messages | less"

 

* To recall the last argument of the last command, use "# !$"

 

"# ifconfig eth0"

"# !$" - this would run "eth0" (and probably tell you that no such command exists)

 

more useful example:

"# cat /var/log/messages"

"# tail !$ | less" - this would run "tail /var/log/messages | less"

 

* To recall another command in history, use "# !#" for recalling history command # something, "# !-#" for the #'ths command from the end (so -2 would be the command before last) you can use $ to call the last argument, or "# !#:#" to recall a particular argument of that particular command

 

examples (useful) :

"# cat /var/log/messages"

maybe its long, control+c out of it

"# wc -l !$" - executes "wc -l /var/log/messages" (tells you the amount of lines in messages log)

"# ^-l" - this removes an argument from the previous command and executes "wc /var/log/messages" (maybe we wanted word count)

"# tail !-3$:p" - prints "tail /var/log/messages" (note :p at the end prints the command)

"# tail !-4:1 | less" - executes "tail /var/log/messages | less"

 

* To search history for a command to recall, use "# !<string>" to recall a command that started with a string, but my favorite method is to use "# !?<string>?"

examples (useful):

 

"# /etc/init.d/httpd stop"

do something that and forget what the last thing you did with the server was

"# !/etc:p" - reminds you of your last command that started with /etc

but what if you just stopped another service too, how do you find your httpd command?

"# !?httpd?:0 start" - this recalls the last command that had httpd in it, using only the first argument "/etc/init.d/httpd" and adding "start" at the end, thus running "# /etc/init.d/httpd start"

 

* To substitute something in the previous command, first instance use "# !!s:/.../.../" or shortened style "# ^...^...", if you need to substitute it globally, use a g option "# !!sg:/.../.../"

 

first instance:

"# ifconfig eth0 && ifconfig eth0 down"

"# !!:s/eth0/eth1/" - would execute "# ifconfig eth1 && ifconfig eth0 down"

 

shortcut:

"# ifconfig eth0 && ifconfig eth0 down"

"# ^eth0^eth1" - would execute "# ifconfig eth1 && ifconfig eth0 down"

 

globally:

"# ifconfig eth0 && ifconfig eth0 down"

"# !!:gs/eth0/eth1/" - executes "# ifconfig eth1 && ifconfig eth1 down"

 

more useful example:

 

i use shortcut to fix commands like this

"# mc config_file ../../backups/configs/."

"# ^mc^mv" - executes "# mv config_file ../../backups/configs/."

 

long i use mostly for global substitution in long and crafty commands like

 

while [ file -f $FILE ] ; do FILE = echo $FILE | awk '{split ($1, filename, "."); idx=1; while(filename[idx] != "log") idx++; idx+=1; if(filename[idx]=="") filename[idx]=0; else filename[idx]=filename[idx]+1; for(idy=1; idy<=idx; idy+=1) if (idx==idy) printf filename[idy]; else printf filename[idy]".";}' ; done

 

With a little practice this can get really fun. I sometimes find myself trying to go for as long as i can, only using history commands, it's a ton of fun :)

 

If you are a Nix person who just learned something exciting, or remembered an old friend, you'll be needing a cold shower about now; above is what I call Nix porn, real down and dirty hardcore (the best kind of porn if you ask me). For the rest of you, get used to crazy Nix admins, Internet is only possible because of them...

Posted

another thing, you can use "# history" to show you the command history, and use "# !#" to use a particular command from history

 

so:

"# history" shows for example

 

286 nmap -h

288 nmap 12.34.56.78 -sP -p 3389

 

i can then do "# !288:p" followed by "# ^sP^sS" to change the option (i have not yet figured out a one-liner for this) to do a syn scan of the port over just determining if the host is up...

 

here is another thing you can do too (i'm stoked, i just figured this out)

 

"# !288:p" - prints "nmap 12.34.56.78 -sP -p 3389"

"# !!:-1 -sS" - executes "nmap 12.34.56.78 -sS"

"# !288:2-:p" - prints "-sP -p"

 

if you add a - before the number, it prints everything to that argument (remember 0 is the command 1 is the first argument), if you add it to the end like !!:1- it subtracts it from the front, but also for some reason does not print the last argument (weirdly enough)

  • 10 months later...
Posted

more random examples:

 

 

# mc /tmp/file.php /var/www/htlm

# ^mc^mv

 

executes: mv /tmp/file.php /var/www/htlm

 

oh but wait there was another typo, what do i do?

 

# mc /tmp/file.php /var/www/htlm

# ^mc^mv^:s/lm/ml/:lol:

 

prints: mv /tmp/file.php /var/www/html

 

now for crazier examples:

# echo "1" | grep "1"

# cat /dev/urandom > /dev/null

# !-2:gs/1/2/:s/grep/egrep/:doh:

 

prints: echo "2" | egrep "2"

 

# echo "2" > file.txt

# echo "1" | grep "1"

# cat !?>?$ !:2-4:s/1/2/:s%gr%egr%

 

executes: cat file.txt | egrep "2"

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...