Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, I'm a newbie to scripting. I've written a small script which checks the input arguments and then scans a directory and counts number of files matching a specific ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    11

    bash script help


    Hi all,

    I'm a newbie to scripting. I've written a small script which checks the input arguments and then scans a directory and counts number of files matching a specific pattern. Below is the code:

    Code:
    #!/bin/bash
    
    #########################################################################################################
    #########################################################################################################
    
    # This script does the following
    
    # 1. Scan a directory that is passed in at runtime for specific filename pattern
    # 2. Count the number of files that match the file name pattern
    
    ##########################################################################################################
    
    if ["$1" == ""] && ["$2" == ""];
    then
    
    echo "Argument 1 and Argument 2 are missing"
    echo "Syntax of the script execution is as follows"
    echo " "
    echo "#################################################################"
    echo "./TestCase_Generic.sh <Absolute_Path_of_Directory> <File_Pattern>"
    echo "#################################################################"
    echo " "
    
    fi
    
    find "$1" -maxdepth 1 -type f -iname \*"$2"*
        count=$(find "$1" -maxdepth 1 -iname \*"$2"* | wc -l)
        echo "$1 ; $count"
    
    echo "There were $count files found, matching pattern $2, in the directory $1"
    ~
    However, when I execute the script, I get the following message along with the expected results:

    Code:
    ./TestCase_Generic.sh: line 13: [/home/zp021418/scripts/: No such file or directory
    /home/zp021418/scripts/def_PCMD_2.txt
    /home/zp021418/scripts/abc_PCMD_1.txt
    /home/zp021418/scripts/xyz_PCMD_3.txt
    /home/zp021418/scripts/ ; 3
    There were 3 files found, matching pattern PCMD, in the directory /home/zp021418/scripts/
    Line 13 is the following:

    Code:
    if ["$1" == ""] && ["$2" == ""];
    How to get rid of the following message? What is causing it?
    Code:
    ./TestCase_Generic.sh: line 13: [/home/zp021418/scripts/: No such file or directory
    Any inputs are greatly appreciated!
    -N.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    try adding spaces around the square brackets, e.g.:

    Code:
    if [ "$1" == "" ] &&  [ "$2" == "" ];
    but as you are testing arguments (positional parameters in Bash), then i typically use:
    Code:
    if [ $# -ne 2 ]; the
      echo "Usage: $0 <FIRST> <SECOND>"
      exit 1
    fi
    the $# is the number of command line args passed to the script.

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    11
    Hi Atreyu,

    Many thanks! That helped solve the problem. Also, thanks for the piece of code. Certainly, useful.

    Regards,
    -N.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    great, glad it is sorted. i'll mark this one as Solved for you. note that you can do this yourself using the Thread Tools link at the top of the page.

Posting Permissions

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