bash: test 1st char of file ? - Slackware

This is a discussion on bash: test 1st char of file ? - Slackware ; I want to have a part of my bash-script testing the first char of $1, or better the string of 4-chars. A sh script on hand, apparently does something like required: > case `echo $1 |sed 's/^.*\.//'` in CASE ststement ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: bash: test 1st char of file ?

  1. bash: test 1st char of file ?

    I want to have a part of my bash-script testing the first char of $1,
    or better the string of 4-chars.

    A sh script on hand, apparently does something like required:
    > case `echo $1 |sed 's/^.*\.//'` in

    CASE ststement [write the $1 file (?1st line only)
    via sed which replaces with nil
    > c|h)

    OF branch "c" or "h" chars
    > indent -kr -pcs "$2"

    do actions for "c" or "h" case
    > ;;
    > C|cc|CC|cxx|CXX|cpp|CPP)
    > astyle "$2"
    > ;;
    > java|JAVA)
    > astyle --style=java --mode=java "$2"
    > ;;

    default CASE
    > esac


    Since the example is examining file extensions, is it
    restricted to examining the tail of $2 ?

    How would it examine the 'head' of $2 ?

    TIA

    == Chris Glur.


  2. Re: bash: test 1st char of file ?

    Thu, 18 Sep 2008 06:06:09 -0500, problems did catÂ*:

    > I want to have a part of my bash-script testing the first char of $1, or
    > better the string of 4-chars.
    >
    > A sh script on hand, apparently does something like required:
    >> case `echo $1 |sed 's/^.*\.//'` in

    > CASE ststement [write the $1 file (?1st line only) via sed which
    > replaces with nil
    >> c|h)

    > OF branch "c" or "h" chars
    >> indent -kr -pcs "$2"

    > do actions for "c" or "h" case
    >> ;;
    >> C|cc|CC|cxx|CXX|cpp|CPP)
    >> astyle "$2"
    >> ;;
    >> java|JAVA)
    >> astyle --style=java --mode=java "$2"
    >> ;;

    > default CASE
    >> esac

    >
    > Since the example is examining file extensions, is it restricted to
    > examining the tail of $2 ?
    >
    > How would it examine the 'head' of $2 ?


    there are several ways to ``examine the 'head' of $2 ($1 I suppose?) ''
    anyway, as you started with sed let's go with it, using the
    same loosy assumes about head/tail marked by a dot:
    echo $1 |sed 's/\..*$//'

  3. Re: bash: test 1st char of file ?

    On Thu, 18 Sep 2008 06:06:09 -0500, problems@gmail wrote:
    > I want to have a part of my bash-script testing the first char of $1,
    > or better the string of 4-chars.


    Some light reading found here
    http://tldp.org/LDP/abs/html/index.html
    for a hot start, check out the B-5. String Operations section.


    > Since the example is examining file extensions, is it
    > restricted to examining the tail of $2 ?


    Tail is a bit of overkill, Try this command

    fn=test.data; echo "${fn##*.}"

    > How would it examine the 'head' of $2 ?


    fn=/home/data/test.data; echo "$(basename $fn)"


  4. Re: bash: test 1st char of file ?

    problems@gmail wrote:
    > I want to have a part of my bash-script testing the first char of $1,
    > or better the string of 4-chars.
    >
    > A sh script on hand, apparently does something like required:
    >> case `echo $1 |sed 's/^.*\.//'` in

    > CASE ststement [write the $1 file (?1st line only)
    > via sed which replaces with nil
    >> c|h)

    > OF branch "c" or "h" chars
    >> indent -kr -pcs "$2"

    > do actions for "c" or "h" case
    >> ;;
    >> C|cc|CC|cxx|CXX|cpp|CPP)
    >> astyle "$2"
    >> ;;
    >> java|JAVA)
    >> astyle --style=java --mode=java "$2"
    >> ;;

    > default CASE
    >> esac

    >
    > Since the example is examining file extensions, is it
    > restricted to examining the tail of $2 ?
    >
    > How would it examine the 'head' of $2 ?
    >
    > TIA
    >
    > == Chris Glur.
    >

    Something like this may help:

    $ TESTSTRING=abcd
    $ echo ${TESTSTRING}
    abcd
    $ echo ${TESTSTRING:0:1}
    a
    $ echo ${TESTSTRING:1:3}
    bcd


    Eric

  5. Re: bash: test 1st char of file ?

    On 2008-09-18, problems@gmail wrote:
    > I want to have a part of my bash-script testing the first char of $1,
    > or better the string of 4-chars.
    >
    > A sh script on hand, apparently does something like required:
    >> case `echo $1 |sed 's/^.*\.//'` in


    To get the first character of $1:

    c1=${1%"${1#?}"}

    To get the first 4 characters:

    c4=${1%"${1#????}"}

    In bash and ksh93, the above can be done with:

    c1=${1:0:1}
    c4=${1:0:4}

    To test the first character:

    case $1 in
    a*) echo a ;;
    b*) echo b ;;
    ## etc..
    esac

    > CASE ststement [write the $1 file (?1st line only)
    > via sed which replaces with nil
    >> c|h)

    > OF branch "c" or "h" chars
    >> indent -kr -pcs "$2"

    > do actions for "c" or "h" case
    >> ;;
    >> C|cc|CC|cxx|CXX|cpp|CPP)
    >> astyle "$2"
    >> ;;
    >> java|JAVA)
    >> astyle --style=java --mode=java "$2"
    >> ;;

    > default CASE
    >> esac

    >
    > Since the example is examining file extensions, is it
    > restricted to examining the tail of $2 ?


    To extract the extension (i.e., the final characters):

    ext=${1##*.}

    To test the extension:

    case $1 in
    *.c) echo "C source file" ;;
    *.h) echo "C header file" ;;
    # etc.
    esac

    > How would it examine the 'head' of $2 ?



    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  6. Re: bash: test 1st char of file ?

    On Thu, 18 Sep 2008 19:55:10 +0000, Chris F.A. Johnson wrote:

    > On 2008-09-18, problems@gmail wrote:
    >> I want to have a part of my bash-script testing the first char of $1,
    >> or better the string of 4-chars.
    >>
    >> A sh script on hand, apparently does something like required:
    >>> case `echo $1 |sed 's/^.*\.//'` in

    >
    > To get the first character of $1:
    >
    > c1=${1%"${1#?}"}
    >
    > To get the first 4 characters:
    >
    > c4=${1%"${1#????}"}
    >
    > In bash and ksh93, the above can be done with:
    >
    > c1=${1:0:1}
    > c4=${1:0:4}
    >
    > To test the first character:
    >
    > case $1 in
    > a*) echo a ;;
    > b*) echo b ;;
    > ## etc..
    > esac


    Chris F.A. Johnson: he represents what info pages could have been :-)

+ Reply to Thread