Double condition

I trying to prevent user from entering empty string and alphabet, but i can do only on of either one cannot seem to make a double condition =.= Any one can help me?? Thank in advance

	
while expr "$sold" : "[a-z]*" > /dev/null ;	#check for int
do
echo "Number only!"
echo "No. of copies sold  : "
read sold
done  
	
while [ "$sold" = "" ]
do
echo "Error! Please enter a value!"
echo "value : "
read sold
done
if `echo $sold` | grep -vq '^[0-9][0-9]*$'  || test -z "$author; then
  echo 'not number in $old or zero length $author" >&2
fi

Erm yazu yours code have the same problem as my, i do not know why but it seem like every time there double condition it will alway have interpretation problem for shell. My code can also work stand along as in with one condition by itself it can work but when i join them up problem occur =.=

What shell?
In dash (posix shell) this code works fine.

menu.sh: line 260: syntax error near unexpected token `fi'
menu.sh: line 260: `fi '

i also using dash(posix shell)

You could try something like this (the code accepts only integers):

while printf 'Number of copies sold: '; do
  read sold
  case $sold in
      ( *[!0-9]*|"" ) 
	    printf >&2 'Invalid input: %s\n' "$sold"  ;;
      ( * ) 
	    break                                     ;;	
  esac
done

# proceed with the value of $sold here ...

Wow radoulov this work like charm can i ask you something i am actually very new to shell. Why when you set a condition for it to check number you put wild card symptom in front and behind

His code is checking for any character(s) followed by a character that is not a number, followed by any character(s). I think you've missed seeing the bang (!) which implies not 0-9:

( *[!0-9]*|"" ) 

So it will issue an error if the string has any character that isn't an error, or is empty.

If you miss that, then yes it is confusing.

1 Like
( [!0-9]|"" )

Y cant i just do this??

 ( [!0-9]|"" )

will just match a single character. If the user enters "a" it will print the error, but if the user enters "aa" it will not. So, by adding the two wildcard characters, you allow it to check for any number of characters, and to match if any of them isn't a digit. So it will reject all of these: "a" "aa" and "20a".

1 Like

Thank!! shell really different from other programming language haha
If you dont mind can i ask you another double condition problem i having??

	while [ "$author" == "" -o "$title" == ""]; #User need to enter either author or title in order for search to begin
	do
	echo "Title : "
	read title
	echo "Author: "
	read author
	done

The logic confirm is correct i want the user to enter either 1 of the field, to check single field for empty string i can do it but that not what i want!! i went google research and saw ppl say must [[Type here]] i try also cannot, i try to play with the ""''`` also cannot =.=
Ya i also try with

while["$author" -eq ""] && ["$title" -eq ""]

Are you using bash or Kshell?

This should work regardless:

while [ "$author" = "" -a "$title" = ""];

You want to loop when both strings are empty (and) rather than one or the other. I'm not sure that the test command (single brackets) supports the double equal (==) which you had in your sample.

If you are using Kshell or bash I think this is a bit more efficient and easier to read:

while [[ "$author" == ""  ||  "$title" == "" ]]
do
.......code here......
done

Hope this makes sense.

1 Like

Ok the 2nd 1 work thank man. Just to clear my doubt how do u know where to put the [] is there a standard way?? and also when do i use == instead of -eq.Lastly u will saying there are different shell is that right?? Different shell different writing style??

Glad that worked.

There are quite a few different shells. Most of them follow the same general syntax (based on the original Bourne shell sh) The most popular are Korn Shell (ksh) and bash. While their syntax may be very similar, they do operate differently in some cases. I prefer ksh as it allows for things that bash doesn't do.

Here is a link to an article on this site that might help answer some of your questions:
Linux.com :: Korn -- an extended shell

The single bracket expression (e.g. [...]) invokes the external test command. Execute [icode]man test[\icode] for more information. It isn't as efficient as using the builtin [[...]] evaluation expressions in ksh/bash, and requires the use of -xx operators for testing strings which I find difficult to read.

As to where to bracket, it depends on what I'm trying to do, but usually I try to have the entire expression inside a single pair of brackets (single or double).

Wah i really got alot more to learn, the way of writing so way different from Java and C..
A big thank to you for your time and effort... thank thank