Find the answer to your Linux question:
Results 1 to 8 of 8
Hello all! I am new to the whole UNIX/Linux world and have a few questions about scripting. I am trying to create a script that takes two directories as parameters ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5

    Getting parameters from command line

    Hello all! I am new to the whole UNIX/Linux world and have a few questions about scripting. I am trying to create a script that takes two directories as parameters and does different checks on the two. The first problem I am having is how do I pass these directories to the script. I have this so far:

    #!/bin/sh
    local dir1=$1
    local dir2=$2

    Not much so far but I want the script to be called like this
    compdir <dir1> <dir2>

    I created 2 directories named dir1 and dir2 and when I type $compdir dir1 dir2
    I get -bash: dir1: command not found. What am I doing wrong here? Another question I have is when I get the directories say I have foo(this is a file) and dirX in dir1 and I want to check and see if they are in dir2?

    Thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Don't use the '$' when you call "compdir". The '$' means that this is a variable. So basically, "$compdir dir1 dir2" means replace "$compdir" with its values and run the command. Well, $compdir has no value, so it is replaced with nothing, and now the command looks like " dir1 dir2", so Bash expects "dir1" to be the command, which it obviously isn't.

    So run the command like this:
    Code:
    compdir dir1 dir2
    And your last question doesn't make much sense. Could you explain it a little bit more fully?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by Cabhan View Post
    And your last question doesn't make much sense. Could you explain it a little bit more fully?
    I guess he means something in the lines of:

    Code:
    for file in "$dir1"/*
    do
      file2="${file/$dir1/dir2}"
      echo "Looking for $file2"
      if [ -r "$file2" ]
      then
        echo "File \"${file/$dir1\//}\" do exist on both directories."
      else
        echo "File \"${file/$dir1\//}\" do exist on dir1, but not dir2 (or it's not readable)."
      fi
    done
    Untested (tm). But it should give you an idea on how to proceed. Of course this is just a simple idea, you might want to extend it. For example, reverse the check and look the files on dir2, to later on check if they exist on dir1.

    I guess this is just an exercise or something, otherwise, you should know that there are tools like diff that can compare files and directories in many ways.

  4. #4
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5
    Thanks guys! This helps alot. I do have a few questions. When checking if the file is in dir2 is there a way I can also check if the date is newer then in dir1. Something like:

    if [ dir1 file date is older then dir2 file date ]
    do something

    The last question I have is when checking for files that are in dir2 and not in dir1. I did something like this but doesnt seem to work:

    for file in "$dir2"/*
    do
    file1="${file/$dir2/dir1}"
    if [ ! "file1" ]
    then
    echo ${file/$dir2\//} " New"
    fi
    done

    Not sure if ! is the right way to check if a file does not exist or not. Thanks for yor help!!!!

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Here's an example on how to check if a file is newer:

    Code:
    #!/bin/bash
    
    fle1="/root/a.a"
    fle2="/root/b.b"
    dte1="`ls -l --time-style="+%F-%R" $fle1 | awk '{print $6}'`"
    dte2="`ls -l --time-style="+%F-%R" $fle2 | awk '{print $6}'`"
    if [[ "$dte1" < "$dte2" ]]
       then echo "fle2 is newer"
       else echo "fle2 is older or the same"
       fi

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    vsemaska, just use -nt under bash. check the bash man page for additional details.

    If you want to do it that way, at least use stat to collect the info, instead of parsing the ls output, which is kind of unreliable and can bring lots of problems.

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    I agree that "stat -c &#37;Z filename" is a very good way to get the timestamp, but stat(1) is not available generally on UNIX systems (another reason to prefer Linux!) so if you want your script to be portable ....

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    If portability is an issue in first place we shouldn't be using bash, but sh, and probably C would be a better option.

Posting Permissions

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