Find the answer to your Linux question:
Results 1 to 5 of 5
I have this script: Code: #!/bin/bash while [ $1 ] ; do lines=$(wc -l $1 | awk '{print $1}') if [ $lines -ge 10 ] ; then echo " $1 ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25

    Script modification

    I have this script:
    Code:
    #!/bin/bash
    while [ $1 ] ; do
    lines=$(wc -l $1 | awk '{print $1}')
    if [ $lines -ge 10 ]  ;  then
    echo " $1 has $lines lines"
    cat $1
    else
    echo " $1 has less than 10 lines"
    fi
    shift
    done
    That checks if a file has more than 10 lines and if so prints its content else it prints that it has less than 10 lines.What I want is the script to ask which file to check(using echo probably) so that the terminal execution will be like that
    Code:
    ./script
    and not
    Code:
    ./script file_name
    Thanks in advance for your advices.

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    You had it earlier on in your other script

    Check it out....

    http://www.linuxforums.org/forum/lin...tml#post603212

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Well,I must be an idiot not to find the similarity.Thanks for yr advice.I have this script now:
    Code:
    #!/bin/bash
    read -p "enter the name of the file: " NAME
    lines=$(wc -l $NAME | awk '{print $1}')
    if [ "$lines" -ge "10" ]  ;  then
    echo "$NAME has $lines lines"
    cat $NAME
    else
    echo "$NAME has less than 10 lines"
    fi
    It's working like a charm...

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can use
    Code:
    wc -l < $FILE
    to get just the count. No need for extra awk.

    Code:
    [ `wc -l < file` -gt 10 ] && echo "greater than 10" || echo "less than 10"

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    25
    Thanks.Quite interesting.I'll keep that in mind for future scripts.

Posting Permissions

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