MoonPoint Support Logo




Advanced Search
August
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
2010
Months
AugSep
Oct Nov Dec


Mon, Dec 29, 2008 2:56 pm

Chopping Strings in BASH

The Bourne-again Shell (BASH) provides built-in mechanisms for extracting substrings from a string.

You can set the value of a variable with myvar="some text". You can view the value of the variable using $myvar or ${myvar}. Putting the "$" in front of the variable name causes BASH to use the value stored in myvar.

$ myvar="some text"
$ echo $myvar
some text
$ echo ${myvar}
some text

There is sometimes an advantage to the format that encloses the variable name in curly braces, i.e. the ${myvar} format.

$ echo "foo$myvar"
foosome text
$ echo "foo$myvarother text"
foo text
$ echo "foo${myvar}other text"
foosome textother text

In the above example, in the instance where the curly braces weren't used, the value of myvar wasn't displayed, because BASH didn't know I wanted myvar rather than myvarother, which has no value assigned to it, so it just dispalyed "foo text". In the second instance where the curly braces were used, BASH could tell I wanted the vaue of myvar and displayed "foo some textother text".

Substrings can be extracted from a string by chopping characters from the beginning and end of a string.

Chopping a trailing character:

You can chop a trailing character from a string in BASH by placing the variable name inside ${ }, such as ${myvar}, and then using %<char>. E.g. suppose myvar has the value "0064092004008999,". To remove the trailing comma from the end of the variable, you could use myvar=${myvar%,}

$ myvar="0064092004008999,"
$ echo ${myvar%,}
0064092004008999

If you wanted to remove the last "9" and all characters that appear after it in the line, you can use "*" in the expression.

$ myvar="0064092004008999,"
$ echo ${myvar%9*}
006409200400899

In the example above, the shortest matching substring is selected and removed, i.e. the "9,". If you wanted to remove the longest matching substring, e.g. every character from the first "9" onwards, you could use

$ myvar="0064092004008999,"
$ echo ${myvar%%9*}
00640

Chopping leading characters:

You can chop leading characters from a string by using # or ##. E.g. suppose myvar has the value "SNMPv2-MIB::sysContact.0 = STRING: John Smith". If you only want the name John Smith, you can use ## to remove the longest substring containing the ":" character. I.e., using myvar=${myvar##*:} wold work. If you instead used only one "#", the shortest matching substring would be removed. I.e., using myvar=${myvar#*:} would return :sysContact.0 = STRING: John Smith, where all characters up to and including the first ":" are removed.

$ myvar="SNMPv2-MIB::sysContact.0 = STRING: John Smith"
$ echo ${myvar##*:}
John Smith
$ echo ${myvar#*:}
:sysContact.0 = STRING: John Smith

References:

  1. Bash by example, Part 1
    Fundamental programming in the Bourne again shell (bash)
    Date: March 1, 2000
    IBM

[/os/unix/bash] permanent link

Sun, Mar 05, 2006 10:53 am

BASH Variables

Some useful variables available in the BASH shell.

Example:


#!/bin/bash

if [ $# -eq 0 ]
then
  echo "Usage: $0 filename"
else
  wc -l $1
fi

The script first checks for whether any argument has been entered on the command line, i.e. whether $# equals zero. If no arguments are present on the command line, the script prints a usage message. The $0 variable holds the name of the script itself. If an argument is entered on the command line, it is presumed to be a filename and the wc command is called to count the number of lines in the file.

So, if the script is named "example", and is called without any options, then the following output would be printed.

# ./example
Usage: ./example filename

If a filename is entered on the command line and that file has 21 lines in it, the following would be printed.


# ./example sample.txt
     21 sample.txt

References:

  1. Linux Shell Programming

[/os/unix/bash] permanent link

Fri, Jul 29, 2005 6:15 pm

Bash Tips

I normally use the bash shell on Unix and Linux systems. A shell is the user interface to the system. The shell on Unix and Linux systems gives you the type of interface you get with a command prompt on Windows systems. On older versions of Windows you would be issuing DOS commands at the command prompt. As you have batch files with DOS, with Unix and Linux shells you can create scripts to automate your work, though you normally get a much richer set of commands than with DOS.

Prior to the development of the bash shell there was a Bourne shell and the name Bourne Again Shell (bash) comes from the name of that prior shell. The bash shell was created by Brian Fox in 1988. He continued to work on it until 1993. Chet Ramey joined Brian in the development of bash in 1989 and Chet continued the work on bash after Brian ceased his development efforts on bash.

I've posted a few bash tips in Bash Tips

[/os/unix/bash] permanent link

Blosxom logo