Results 1 to 5 of 5

Thread: Problem with script

  1. #1
    Join Date
    Oct 2008
    Beans
    68

    Problem with script

    Hi
    I am trying to make a script that tells me if the file "Hello" appears more than 5 times.

    I have the script the following way

    Code:
    var = ls | grep -c Hello 
    if [var >5];then
       echo "It appears more than 5"
    else
       echo "It appears less than 5 times"
    fi
    I know that the problem is on the var. But I don't how to assigned the value that grep returns.

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Problem with script

    Code:
    var=$(ls | grep -c Hello)
    No spaces!
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Oct 2008
    Beans
    68

    Re: Problem with script

    Solve
    Thanks

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Problem with script

    Code:
    [var >5]
    that won't work either
    here you need everything separated by spaces ( [ is a command, var is first param, > 2nd, 5 third, ] last )
    besides var should be used with $ ($var) and > should be replaced with -gt (greater than)
    alternatively if you are using bash you can use
    Code:
    if (( var > 5 ))
    (()) switches to integer mode but it's not too portable to other shells.

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Problem with script

    The output of ls if formated for humans. It will cause bugs in scripts. You should use globs instead:
    Code:
    i=0
    for _ in *Hello*
    do
       #BASH
       ((i++))
       #POSIX
       #i=$((i+1))
    done
    
    #BASH
    if (( i>5 ))
    #POSIX
    #if [ $i -gt 5 ]
    then
       :
    fi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •