Quote:>Hi, I am newbie in unix shell programming.
>Can someone tell how to check that a variable that have a certain text?
> e.g.
> M=File.ddv
> if [ ${M} = "*.ddv" ]; then
> echo This is ddv file
> fi
> Obviously above does not work.
> How can do it with sed...?? [A-Za-z0-9]*.ddv ??
This is definently not a job for 'sed' (however
see below), use the shell built-in 'case';
case "$M" in
*.ddv) echo this is a ddv file;;
esac
There is documentation for 'case' on the shell
man-page (man sh).
If you really wanted to use 'sed' to match these strings
you would have to put a backslash before the period
(because otherwise a period will match any character)
also you would have to anchor it with ^ and $ to make
sure it matched only the whole string;
/^[A-Za-z0-9]*\.ddv$/
But making 'sed' somehow to tell the shell to act
one way or another because it matched the string
would just get plain ugly...
print the string if it matches (like grep);
echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d'
count the number of lines;
echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d' |wc -l
if the number of lines is 1 then its okay;
if [ `echo $M |sed '/^[A-Za-z0-9]*\.ddv$/!d' |wc -l` -eq 1 ]
then
echo its a ddv file
fi
see what I mean !? ;) it could be done by instead getting
'sed' to print 'true' or 'false' and using that as a command
(and maybe could be made a bit shorter) but its not really
the tool for the job when you have 'case' (at least here,
where you only really need a shell pattern and not a regular
expression)
'grep' is (slightly) better here because it returns 'true' if
it finds a match and 'false' if it doesn't so you can
do away with more than half of the above but have to
add a >/dev/null to stop it printing stuff out;
if echo "$M" |grep '^[A-Za-z0-9]*\.ddv$' >/dev/null
then
echo its a ddv file
fi
byefrom,
: ${L} # http://lf.8k.com:80