Validation of variable

Hey people, I got small little error which says I entered too many argument in the if else list.
The stock contains a list of information.
The 4th field consist of the quantity available, so I'm trying to alert the user that the stock is less than 3 and needed to restock.
Is this the correct way to do, cuz I'm having errors..

function alert_stock
{
    qtyA=`cat stockDB.txt | cut -d: -f4`
    if [ $qtyA -lt 3 ]; then

    echo $qtyA

    fi
}

Almost always use quotes around variables. And you need arithmetic operators for arithmetic comparison and you need quote them (because of shell redirection operators). So try:

[ "$qtyA" \< 3 ] 

PS Please give the exact error message and after what action you got it.

1 Like

haha thx it works!! but it show one show chunk of inputs in a line but included all the list of stocks which even has more than 3.
I thought that after cutting the amount and display, everything will be less than 3.

---------- Post updated at 11:48 PM ---------- Previous update was at 10:47 PM ----------

Ok I done another way using awk.
It will print for the first field name and the fourth field which is the stock.
I know something wrong with the $4<3.
Anyone can enlighten me?
All the script want is to print anything that has less than 3.

{  
    awk -F: '{printf "%-1s,%-10s\n", $1, $4<3}' stockDB.txt
}

---------- Post updated at 11:48 PM ---------- Previous update was at 11:48 PM ----------

Ok I done another way using awk.
It will print for the first field name and the fourth field which is the stock.
I know something wrong with the $4<3.
Anyone can enlighten me?
All the script want is to print anything that has less than 3.

{  
    awk -F: '{printf "%-1s,%-10s\n", $1, $4<3}' stockDB.txt
}

either:

function alert_stock
{
    qtyA=`cat stockDB.txt | cut -d: -f4`
    if [ "$qtyA" -lt 3 ]; then

    echo $qtyA

    fi
}

the above performs the numerical comparison (unlike '<' which is alphabetical).
OR

awk -F: '{printf "%-1s,%-10s\n", $1, int($4)<3}' stockDB.txt

some awk-s are more picky than the others.

1 Like

Oh yes :(. Too much perl programming :). Thank you.

Sample A, 0
Sample B, 0
Sample C, 0
.
.
.

All About Ubuntu, 1

The stock currently for All About Ubuntu actual amount should be 2 but the output is 1.

Haha it does work somehow., However it shows the list of amount with 0 at the side.

I've tried

awk -F: '$4<='3'' BookDB.txt

It works it pick up every value that has less than 3. But I need to rename it properly.

Current output:
All About Ubuntu:Ubuntu:30:2:2

Desired output
All About Ubuntu, 2

---------- Post updated at 01:05 AM ---------- Previous update was at 12:23 AM ----------

Urm like previously explained above, now I can show the specific amount which is less than 10.
However now show all the 5 field in the output.
All the output wants is to show the first field(Name) and the 4th field(Current Quantity).
I tried to use printf together with this, but failed.. :wall:

awk -F: '$4<='10'' BookDB.txt
awk -F: '$4<=10 {print $1, $4}' OFS=:  BookDB.txt
1 Like

Thanks it works wonder