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

Most Popular

  • osTickets > Reports v4.2 (18056)
  • HTPC > Setup Windows 7 as a Media Center with XBMC (10962)
  • osTicket > View headers for original email message (4173)
  • WebDev > Allow PHP in Wordpress Widgets (3615)
  • Web Dev > Password protect webpage(s) with PHP & LDAP (2475)

Posts in category BASH

BASH > ‘sed’ command examples.

Nov15
2010
Leave a Comment Written by Scott Rowley

Another list that will grow as I need it…

Remove blank lines from output

sed '/^$/d'

Replace data with new data

sed 's/DATA/NEWDATA/g'

Replace multiple spaces with single space

sed 's/  */ /g'

Replace first instance of string

sed -e 's/pattern/REPLACEMENT/1'
ajax loader
Posted in sed - Tagged blank, data, empty, output, replace, sed
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > ‘find’ examples

Nov12
2010
Leave a Comment Written by Scott Rowley

This will be a growing list as I find commands I need…

Find files of .ext that are 0 bytes and rm them.

find . -name "*.ext" -size 0 -exec rm {} \;
ajax loader
Tagged byte, example, find, rm, shell
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Variable checking

Nov11
2010
2 Comments Written by Scott Rowley

Check the variable is a whole number

if [ $VARIABLE -eq $VARIABLE 2> /dev/null ]; then
echo $VARIABLE is a number
else
echo $VARIABLE is not a number
fi

Check the variable is not empty

if [ $VARIABLE != "" ] ; then
 <code_to_execute>
else
 echo "Error: VARIABLE is empty"
fi

or as provided by solstice:

if [ -z "$VARIABLE" ] ; then
 echo “VARIABLE is empty”
else
 echo “VARIABLE is not empty”
fi

or -n to test if it”s not null

if [ -n "$VARIABLE" ] ; then
 echo “VARIABLE is not empty”
else
 echo “VARIABLE is empty”
fi

Check the variable is alpha only

if [[ "$VARIABLE" =~ ^[a-zA-Z]+$ ]] ; then
echo "Variable contains letters only"
else
echo "Error"
fi
ajax loader
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Connect to another user’s console terminal using ‘screen’

Nov02
2010
Leave a Comment Written by Scott Rowley

Recently, I was helping another Admin and I wanted to be able to share our screens but our IS department won’t allow for it. Being that we were working in a terminal session I decided to go this route instead.

Needed:
- Screen
- Local account on host computer/server for remote user

Install screen

sudo apt-get install screen

Set the screen binary (/usr/bin/screen) setuid root. By default, screen is installed with the setuid bit turned off, as this is a potential security hole.

sudo chmod +s /usr/bin/screen
sudo chmod 755 /var/run/screen

The host starts screen in a local xterm, using the command screen -S SessionName. The -S switch gives the session a name, which makes multiple screen sessions easier to manage.

screen -S screen-test

The remote user (remote_user) uses SSH to connect to the host computer (host_user).

ssh remote_user@server

The host (host_user) then has to allow multiuser access in the screen session via the command ^A :multiuser on (all ‘screen’ commands start with the screen escape sequence, ^A).

^A
:multiuser on

The host (host_user) must grant permission to the remote user (remote_user) to access the screen session using the command ^A :acladd user_name where user_name is the remote user’s login ID (remote_user).

^A
:acladd remote_user

The remote user can now connect to the hosts ‘screen’ session. The syntax to connect to another user’s screen session is screen -x host_user/sessionname.

screen -x host_user/screen-test
ajax loader
Posted in screen - Tagged connect, screen, share, terminal, user
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > How to install perl modules through CPAN on Ubuntu

Oct26
2010
Leave a Comment Written by Scott Rowley

Install all dependent packages for CPAN

sudo apt-get install build-essential

Invoke the cpan command as a normal user

cpan

Once you hit on enter for “cpan” to execute, you be asked of some few questions. To make it simple for yourself, answer “no” for the first question so that the latter ones will be done for you automatically.

Enter the commands below

make install
install Bundle::CPAN

Now all is set and you can install any perl module you want. examples of what installed below:

cpan prompt> install  IO::File
cpan prompt> install  Net::SMTP_auth
cpan prompt> install Email::MIME::Attachment::Stripper
cpan prompt> install Mail::POP3Client
ajax loader
Tagged cpan, install, module, Perl
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Remove tabs from output

Oct26
2010
Leave a Comment Written by Scott Rowley

In order to remove tabs from your output (say to get a specific variable) use the ‘tr’ command, for example

In my zone.file the line containing “Serial” looks like the following:
If I performed

grep Serial zone.file

I would get the following output

                       2009110904      ; Serial (yyyymmdd##)

You can’t see them here but there are several tabs before that date. In my example I want to get just the actual serial number (2009110904). To do this I’ll first strip off the “; Serial…” information;

grep Serial zone.file | awk -F; {'print $1'}

This command is telling it to look for the ; special character and break up the output there, then from there it is told to print the first column of information this results in:

                       2009110904

The problem here is that if we want this to be our variable we are actually working with “     2009110904″ instead of the desired “2009110904″ In this particular case we know those are tabs not spaces. So we’ll need to use tr in order to remove those tabs:

grep Serial zone.file | awk -F\; {'print $1'} | tr -d 't'

This will return to us exactly what we want

2009110904
ajax loader
Tagged awk, grep, tab, tr
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Hello World Script

Oct26
2010
Leave a Comment Written by Scott Rowley

So you’ve either recently come to the Unix world or you are finally getting around to some shell scripting to ease your daily operations (Say hi Jim!). So for starters what *IS* bash scripting? Scripting in general is making a program or “script” do all or most of your work for you. For instance if you wanted to find every instance of the word “hosting” in a particular file that is 30,000 lines long. That would take a LONG time doing that by hand and you would likely miss several along the way (being a fallible human and all). So instead we would use a grep (or similiar command) from the command line.
Well that may be easy enough for just one command but what if we then need to check all of those lines that are found and seperate them by date or some other criteria, then sort them out etc? For this we will use scripting. We can also use scripting for events that we want to take place every single day, whether it be simple or complex. This can be done via a cronjob, but we can get to that in a later article. So first off we’ll need to find where our “bash” resides…

which bash > helloWorld.sh

“which bash” will give us the location of the bash that is being used within our path. The > will send that result into a new file titled “helloWorld.sh” that we will use for our script. Now if we open up that file we will have something like the following:

/usr/bin/bash

Nothing else should be in our file at this point. This is good but not quite what we need. We need to add #! in front of this. This tells our script that we will be using /usr/bin/bash as our shell to run the rest of this script.

#!/usr/bin/bash

To complete our very first script all we’ll need to do is enter in the following a few lines (doesn’t matter how many) after our #!/usr/bin/bash line, like so:

#!/usr/bin/bash

echo "Hello World!"

Now we have our script ready, we just need to execute it. There are two ways to accomplish this, first we can simply chmod the file to give it the execute (x) permission. For simplicity sake we’ll say its:

chmod 755 helloWorld.sh

The 7 sets it so that the owner (you) can do anything with the file, the first 5 sets it so that anyone in its group can read and execute it and likewise for the last 5 but for this is for everyone, not just the group. More on chmod and chown in future articles.

Now that we have our file chmod’d to be executable we can run it right from where we are by typing

./helloWorld.sh

and our output should be simply:

Hello World!

As mentioned there are two ways to execute the file. For the second method we could type the following and not have to worry about permissions (only ownership – but since we own it we really don’t have to worry about that either).

/usr/bin/bash ./helloWorld.sh

And, again, our output would be the same:

Hello World!

Congratulations, you’ve just made your first Bourne Again Shell (Bash) Script.

ajax loader
Tagged chmod, command, echo, hello world, script
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Get all but the last field

Oct26
2010
Leave a Comment Written by Scott Rowley

Recently I had need of cutting up domain names such as domain.com, this is easy enough for as simple a domain as this. However, the problem arises when you need to cut up something more like “education.k12.ia.us”. In order to get the extension alone in either case you can do the following:

echo domain.com | awk -F. {'print $2'}
com

Now normally if you have a domain as simple as example.com with only one period then you can can just do the opposite of this and print $1:

echo domain.com | awk -F. {'print $1'}
domain

The problem with more complex URLs is that awk’s field delimiter is now looking at multiple periods to look at, could be just one or it could be two, three, four or more.

To get the final extension in this case then we’ll use the following:

echo education.k12.ia.us | awk -F. {'print $NF'}
us

Then for the rest of it we’ll have to reverse it, then cut off the first field then reverse it again:

echo education.k12.ia.us | rev | cut -d. -f2- | rev
education.k12.ia
ajax loader
Tagged awk, delimiter, domain, extension, field
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > if ARRAY [does not] contains string

Oct26
2010
Leave a Comment 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
ajax loader
Tagged array, contain, string
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Set DELETE key in Putty/Poderosa/SecureCRT

Oct26
2010
Leave a Comment Written by Scott Rowley

For Poderosa/Putty
- Create or edit the ~/.inputrc file with the following code:

"\e[3~": delete-char

For SecureCRT

Click Tools
Keymap Editor
Click the "DEL" button
Click Map Selected Key
enter in "\e[3~" (no quotes)
Hit OK
Save
ajax loader
Tagged delete, key, poderosa, putty, securecrt, terminal
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« Older Entries Newer Entries »

Be Heard!

Authors needed! Feel like sharing your tech wisdom with the world? We are looking to expand our writer base and would love to hear from you. We need articles on any relevant technology/software/media/howto/etc (Well...lets at least hold to the legal stuff ;)

Just email scott (at) sudobash (dot) net

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

Sudo Bash Member sites

Des Moines, Iowa Karate Classes
Iowa MMA Tournaments
Iowa SAR
Ooo-er
5point Studios Tattoo & Piercing

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

RSS HowToGeek

  • Week in Geek: Aero Glass Feature will be Removed from Windows 8
  • How to Speed Up Web Browsing with Search & Bookmark Keywords
  • Geek Trivia: Pagers Were First Developed To Assist Which Professionals?
  • The Best Tips and Tricks for Using Email Efficiently
  • Desktop Fun: Starry Skies Wallpaper Collection Series 2

RSS TheGeekStuff

  • UNIX / Linux Processes: C fork() Function
  • How to Calculate IP Header Checksum (With an Example)
  • How to Encrypt Your Bash Shell Script on Linux Using SHC
  • Intro to DOCSIS Architecture, CM CMTS Protocol for Cable Modems
  • Ettercap Tutorial: DNS Spoofing & ARP Poisoning Examples

RSS LifeHacker

  • DIY Maple-Flavored Pancake Syrup [Breakfast]
  • This Week's Top Downloads [Download Roundup]
  • Make Mini Dutch Baby Pancakes in a Muffin Tin [Breakfast]
  • Pour Vinegar Down Your Drain Every Three Months to Keep Clogs Away [Video]
  • Use the Jelly Pocket Method for a Better Drip-Free PB&J [Food Hacks]

EvoLve theme by Blogatize  •  Powered by WordPress Sudo Bash
By Geeks - For Geeks

Back to Top