Bash

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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: ,

Sunday, September 27th, 2009 Bash No Comments

Make Shell Scripts Executable By Default

If you use vim to write shell scripts, you might want to save the “chmod +x” command after saving the script.

By adding the following line to your vimrc file (typically, it’s located at ~/.vimrc), scripts will automatically become executable.

au BufWritePost * if getline(1) =~ “^#!” | if getline(1) =~ “/bin/” | silent !chmod a+x <afile> | endif | endif

(meaning, if the file includes #! with “/bin/” in the path, apply “chmod a+x” on this file).

via shell-fu.

Have fun ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , ,

Saturday, September 26th, 2009 Vim 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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm 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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm 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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm 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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Friday, July 3rd, 2009 Bash No Comments

A Bash Script to Convert brainfuck Code Into C

Brainfuck is a minimalist Turing Complete language. You can read about it here.

The following bash script takes a file with brainfuck code (legal characters are >< ,.+-[] any other characters are ignored), and generates its C equivalent code, which can then be compiled with a C compiler and executed.

The output of this script lacks indentation. If you insist on having the C code indented you can achieve this by passing the output through a pipe to “indent”, “astyle” or similar programs.

The script should be called in the following way:

./bf.sh code.bf > code.c

Where code.bf is the file containing the brainfuck code.

› Continue reading

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , ,

Sunday, June 14th, 2009 Programming No Comments

Share Code Snippets Easily From the Command Line

This is a great tip I found at commandlinefu to easily share code snippets write from the command line.

First, you may want to add this to your aliases file:

1
alias share='curl -F "sprunge=<-" http://sprunge.us | xclip'

(If you don’t have the xclip program, you can easily install it from your package manager.)

Now, say that you have a c program named “hello.c”, to share it, type this (after reloading your aliases file of course):

cat hello.c | share

Now, the code is already in the internet, and the URL is in your clipboard. Go to firefox and click the middle mouse button in the URI area. You’ll see a URL similar to this:

http://sprunge.us/BaFS



To view the code with syntax highlighting and line numbers, just append “?c? (or “?lang” where ‘lang’ is the relevant programming language). So the final URL should look like this:

http://sprunge.us/BaFS?c

Enjoy ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , , ,

Saturday, June 13th, 2009 Programming No 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 Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Friday, June 12th, 2009 Bash No Comments

Edit The Command Line With Vim

This is a quick (and great) tip I found at Daily Vim:

Open a linux terminal, and type some (long) command.
Now type [ctrl]+[x] and then [ctrl]+[e]
The command should be moved now to a vim window.
Edit the command (fix typos, change parameters, etc..) and save.
The command will now be executed.
Enjoy ;)

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to Reddit Post to StumbleUpon

Tags: , ,

Monday, June 1st, 2009 Vim No Comments
my email
Already a member?
Login
Login using Facebook:
Last visitors
my photo
Hi,
My name is Amir Watad. I have a BSc. in biomedical engineering from The Biomedical Engineering school , Technion , Israel, and am currently studying for a BSc. in electrical engineering at The Electrical Engineering school , Technion , Israel.
I work at the verification dept. 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.
March 2010
S M T W T F S
« Jan    
 123456
78910111213
14151617181920
21222324252627
28293031  
Firefox Logo Crystal
SEO Powered by Platinum SEO from Techblissonline

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