Unix Shell - bash script question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > bash script question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash script question
nibbio

2006-10-22, 1:16 pm

Hi, I have two simple, maybe trivial, question about bash script .
1) I want to do a script that receives 3 arguments, and the second one
had to be a number. I tried with
if [ $2 != [ 0-9 ] ]
then
exit 1
but it doesn't work.
2) there is any way to insert few lines (always the same) at the
begining of a text document? I read that the
'ins' command would do this but i'm not able to use it in a script.
Thanks

Janis Papanagnou

2006-10-22, 1:16 pm

nibbio wrote:
> Hi, I have two simple, maybe trivial, question about bash script .
> 1) I want to do a script that receives 3 arguments, and the second one
> had to be a number.


Test a number or a digit?

> I tried with
> if [ $2 != [ 0-9 ] ]


if [[ $2 != [0-9] ]]

or use [[:digit:]] instead of [0-9] .

You may also use case instead of the [[...]] test operator;

case $2 in
[0-9]) echo match a digit ;;
*) echo not a digit ;;
esac

> then
> exit 1
> but it doesn't work.
> 2) there is any way to insert few lines (always the same) at the
> begining of a text document? I read that the
> 'ins' command would do this but i'm not able to use it in a script.


I don't know about any 'ins' command; that's likely non-standard.

cp yourfile yourfile.bak && cat headerfile yourfile.bak >yourfile

If you don't want or have the standard header in a header file you
may use an explicit string

.... && { echo "whatever..." ; cat yourfile.bak ; } >yourfile

You may or may not want to remove the backup files later.

Janis

> Thanks
>

Chris F.A. Johnson

2006-10-22, 1:16 pm

On 2006-10-22, nibbio wrote:
> Hi, I have two simple, maybe trivial, question about bash script .
> 1) I want to do a script that receives 3 arguments, and the second one
> had to be a number. I tried with
> if [ $2 != [ 0-9 ] ]
> then
> exit 1
> but it doesn't work.


To match patterns, use the case statement, not the test command
(a.k.a., [ ... ]):

case $2 in
*[!0-9]*) exit 1 ;;
esac

Bash has an expression evaluator, [[ ... ]], which will match
patterns, but it's non-standard.

> 2) there is any way to insert few lines (always the same) at the
> begining of a text document? I read that the
> 'ins' command would do this but i'm not able to use it in a script.


There is no 'ins' command, but you could easily write your own.

cp FILE FILE.bak
{
printf "%s\n" "$LINES_TO_PREPEND"
cat FILE.bak
} > FILE

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
nibbio

2006-10-23, 1:16 pm

Thanks to all!!! the advices work fine

Chris Mattern

2006-10-24, 7:17 pm

In article <q7ks04-bht.ln1@xword.teksavvy.com>, Chris F.A. Johnson wrote:
>
> Bash has an expression evaluator, [[ ... ]], which will match
> patterns, but it's non-standard.
>

[[ ... ]] is not a bashism. It is in all versions of Kornshell, and
is part of the POSIX shell definitions. It's not in Bourne shell,
but if you restrict yourself to Bourne shell, there's gonna be a
lot of things you can't do. There are very few systems these days
that don't have either Kornshell or bash. In fact, [ ... ] is
considered deprecated in those shells.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Daniel Rock

2006-10-24, 7:17 pm

Chris Mattern <syscjm@sumire.eng.sun.com> wrote:
> In article <q7ks04-bht.ln1@xword.teksavvy.com>, Chris F.A. Johnson wrote:
> [[ ... ]] is not a bashism. It is in all versions of Kornshell, and
> is part of the POSIX shell definitions.


It is allowed but not required:

http://www.opengroup.org/onlinepubs....html#tag_02_04

See also:

http://www.opengroup.org/onlinepubs...l#tag_04_140_18

The KornShell-derived conditional command (double bracket [[ ]]) was removed
from the shell command language description in an early proposal.

--
Daniel
Chris F.A. Johnson

2006-10-24, 7:17 pm

On 2006-10-24, Chris Mattern wrote:
> In article <q7ks04-bht.ln1@xword.teksavvy.com>, Chris F.A. Johnson wrote:
> [[ ... ]] is not a bashism. It is in all versions of Kornshell, and
> is part of the POSIX shell definitions.


It is not part of POSIX:

The following words may be recognized as reserved words on some
implementations (when none of the characters are quoted),
causing unspecified results:

[[ ]] function select

> It's not in Bourne shell, but if you restrict yourself to Bourne
> shell, there's gonna be a lot of things you can't do. There are very
> few systems these days that don't have either Kornshell or bash. In
> fact, [ ... ] is considered deprecated in those shells.


[ ... ] is not deprecated by the standard or by bash.


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com