Bash

Bash Tip – Separate a Bash Variable From Surrounding Letters

Sometimes, you might find yourself needing to concatenate the value of a bash variable with a string.
I think it’s best demonstrated with (a somewhat artificial) example:

1
2
3
y=h
echo $yome #won't work. will print the value of the variable "yome" which is a null string (since it's not set)
echo ${y}ome #works. will print "home"

That’s it, the trick is to use the curly brackets ${var} to separate the variable from its surrounding.

Have fun ;-)

source: @bashcookbook

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, April 24th, 2010 Bash No Comments

Re-Use A Bash Command With Different Parameters

Suppose you have typed and executed this command in your Linux shell:

1
./script_a.sh 1.23 && ./script_b.sh 1.23 && ./script_c.sh 1.23.45

Now you want to run the same command, but with 2.34 instead of 1.23
A nice way to do it is this:

1
!!:gs/1.23/2.34

Meaning, run the last command (!! is also called ‘bang bang’, and it’s substituted by the last command you executed), and replace every instance of 1.23 by 2.34

via Unix Bash Scripting.
Have fun ;)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Sunday, September 27th, 2009 Bash No Comments

Trimming Bash Variables – A Summary

This post should summarize the subject of stripping out bash variables, we already talked about on previous posts

Let’s say we have a bash variable (say x), which stores a string (say “ExExampleStringStr”)

Then we can do the following manipulations:

1.

1
y=${x%Str*}

This will trim out the shortest match of the pattern “Str*” from the end of the string.
Thus, y will have the value “ExExampleString”.
› Continue reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags:

Saturday, September 5th, 2009 Bash No Comments

Strip Leading Characters Off A String

Say you have a bash variable (say x) storing the string “MyLongString”

This bash command:

1
    echo ${x##My}

Will strip the leading string “My” off the original string. And thus the output will be:

LongString

via @bashcookbook

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Friday, September 4th, 2009 Bash No Comments

Strip the File Name Suffix off a Bash Variable

Suppose you have a bash variable storing a file name, say x =file.jpg

You can use this substitution command in order to strip out the file suffix off the string:

1
echo ${x%.*}

The output will be the base name of the file without the suffix.

via @bashcookbook

Enjoy ;)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Tuesday, September 1st, 2009 Bash No Comments

Create Multiple Nested Directories in Linux

Did you know that you can create a complete directory tree with one command?

Let’s start with a simple example:

mkdir -p a/b/c

will create this tree:

`-- a
    `-- b
        `-- c

And any of these:

mkdir -p a/{b1,b2}/c

or

mkdir -p a/b{1,2}/c

Will create this tree:
› Continue reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , ,

Friday, July 3rd, 2009 Bash 2 Comments

Tiny Bash Functions to Convert Between Numeric Representations

I have these functions in my aliases file. They make it easy to convert between hex, binary and decimal representations of numbers. You might find it useful.

Just add these to your aliases file (imported from ~/.bashrc):

1
2
3
4
5
6
function h2d { echo "obase=10; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
function b2d { echo "obase=10; ibase=2; "$*"" | bc; }
function b2h { echo "0x$(echo "obase=16; ibase=2;"$*"" | bc)"; }
function d2b { echo "obase=2; ibase=10; "$*"" | bc; }
function d2h { echo "0x$(echo "obase=16; ibase=10; "$*"" | bc)"; }

Now you should be able to use it like this:

h2d 0xff

The output will be 255 in this case.

h2b is for “hex to binary”, the others are similar (h: hex, b: binary, d: decimal)

Enjoy ;)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , ,

Friday, June 12th, 2009 Bash No Comments

Renaming Files In Linux

In a previous post, I wrote about converting strings from camelCase to undescore delimited format.

In this post, we’ll do the same but for file names. We’ll use the command line program “rename” for this purpose.
Here we go:

1. Convert all file names in current directory which are of the form “myFileName” or “MyFileName” into the form of “my_file_name”:

1
rename 's/(.)([A-Z])/$1_\l$2/g' * && rename 's/([A-Z])/\l$1/g' *

2. Convert all file names in current directory which are of the form “my_file_name” into the form “myFileName”:

1
rename 's/([a-z])_([a-z])/$1\u$2/g' *

3. Convert all file names in current directory which are of the form “my_file_name” into the form “MyFileName”:

1
rename 's/([a-z])_([a-z])/$1\u$2/g' * && rename 's/^([a-z])/\u$1/g' *

Enjoy ;)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, May 30th, 2009 Bash 1 Comment

Convert camelCase to Underscores Using sed

This short post deals with converting strings of the form camelCase or CamelCase into camel_case, and vice versa. These are three different popular naming conventions for variable/function/class names.

Convert CamelCase or camelCase to camel_case:

1
sed -e 's/\([A-Z]\)/_\l\1/g' -e 's/^_\([a-z]\)/\1/g' file.txt

Convert camel_case to camelCase

1
sed -e 's/_\([a-z]\)/\u\1/g' file.txt

Convert camel_case to CamelCase:

1
sed -e 's/_\([a-z]\)/\u\1/g' -e 's/^\([a-z]\)/\u\1/g' file.txt

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: ,

Saturday, May 30th, 2009 Bash 5 Comments

Sed and AWK – A quick reference

Sed (Stream Editor) and AWK (First letters of the surnames of its authors) are very powerful *nix tools for manipulating strings and text files. They combine the power of regular expressions with the power of a programming language for this aim.
Here are two quick references for doing many operations with a one-line code of sed and AWK. (Both are by the same author – Eric Perment):
› Continue reading

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , ,

Saturday, May 9th, 2009 Bash No Comments
my email
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and a BSc. in electrical engineering from The Electrical Engineering school , Technion , Israel.
I'm a Verification Engineer in Mellanox Technologies Ltd.
I love Linux, the Command Line and the OpenSource Community.
I used to write Poems (Arabic) when I was able to find time for this.
February 2012
S M T W T F S
« Sep    
 1234
567891011
12131415161718
19202122232425
26272829  
SEO Powered by Platinum SEO from Techblissonline

Twitter links powered by Tweet This v1.7.4, a WordPress plugin for Twitter.