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 ...
- 07-24-2008 #1Just 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
- 07-25-2008 #2
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:
And your last question doesn't make much sense. Could you explain it a little bit more fully?Code:compdir dir1 dir2
DISTRO=Arch
Registered Linux User #388732
- 07-25-2008 #3Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
I guess he means something in the lines of:
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.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
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.
- 07-25-2008 #4Just 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!!!!
- 07-25-2008 #5Linux 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
- 07-25-2008 #6Linux 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.
- 07-27-2008 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I agree that "stat -c %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 ....
- 07-28-2008 #8Linux 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.


Reply With Quote
