• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts tagged BASH

BASH> if ARRAY [does not] contains string

Oct26
2010
Written by Scott Rowley

Check if an array contains a given string:

teams=(chiefs broncos chargers raiders)
if [[ ${teams[*]} =~ broncos ]]
then
echo "VICTORY!"
fi

Or, if you don’t want it to contain the string then:

teams=(cardinals 49ers seahawks rams)
if [[ ${teams[*]} =~ broncos ]]
then
echo "This conference sucks"
else
echo "VICTORY!"
fi
Tagged array, contain, string

Perl> Get IP only from logfiles

Oct26
2010
Written by Scott Rowley

Being an ISP admin, I have regular need to find IP addresses listed in large logfiles. The following will grab anything that appears to be an IP and list it out for you, from there you can sort or whatever you want to do with the list.

perl -ne 'print "$&n" while m#d+.d+.d+.d+#g' <logfile>

Or just throw it in a bash script for easier remembering:

getip.sh

#!/bin/bash
perl -ne 'print "$&\n" while m#\d+\.\d+\.\d+\.\d+#g' $1;
Posted in Perl - Tagged IP, log, logfile, Perl, print

Emacs> Set delete key in emacs

Oct26
2010
Written by Scott Rowley

I really hate that the delete key backspaces. I’m sure there is a lot of pointless posturing on how this is the ‘old school’ way. Well… 1) I don’t care 2) I’m new school and I want a backspace key AND a delete key…
Add the following line to your ~/.emacs file:

(global-set-key (kbd "") 'delete-char)
Posted in Emacs - Tagged backspace, delete, Emacs, key

BASH> Run command from root as another user

Oct26
2010
Written by Scott Rowley

Every once in a awhile you may find you need to run a command as a different user than yourself. In order to do so you will first need to be root, then perform the following:

su <username> -c '<command1>; <command2>;'
Tagged command, root, su, user

BASH> Redirect output to standard out AND file

Oct26
2010
Written by Scott Rowley

Eventually in scripting you are likely to want to output to both the screen (standard out) and to a file. This makes it convenient to see whats getting “logged” without needing the extra step of cat’ing the file.

For example:

ls -al | tee ls_al_result.txt

This will give you your normal ‘ls -al’ results but also feed them to your new file.

Tagged ls, redirect, standard out, tee

BASH> Tail multiple files & highlight

Oct26
2010
Written by Scott Rowley

Tail multiple files at once:

tail -f log1.log log2.log

If you want to highlight something when doing ‘tail -f’ you can use the following command:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[7;1m$133[0m/g;'

Or if your terminal supports colours, such as linux terminal, you can use this:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[46;1m$133[0m/g;'

And if you want it to beep (annoy you!) on a match use this:

tail -f /var/log/logfile | perl -p -e 's/(SEARCH)/33[46;1m$133[0m07/g;'

Another option using sed instead:

tail -f /var/log/logfile | sed "s/(SEARCH)/^[[46;1m1^[[0m/g"

Note that in the last example you have to actually type “cntl-v cntl-[” in place of “^[”

For a full list of control characters on Linux see:

man console_codes
Tagged highlight, sed, tail
Newer Entries »

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top