Name: phgct2004 Date: May 18, 2004 at 20:13:28 Pacific Subject: multiple filename as argument OS: Redhat 9.0 CPU/Ram: 866/128
Comment:
hi all,
i have face new prolem in writing my bash script. my problem is that i want to write a script that will accept any number of arguments and return the newest file from the list. and i only manage to compare the file using
file1 -nt file2
the problem is that how can i write a script that can accept multiple filename as argument and have a check whether such file is exit or not.
i have manage to check the multiple file as argument that key in by the user already but the problem is that my coding that compare the newest file not work when the argurment is passing. below is my coding and can anybody tell me what wrong with my bash script and have a correction for me. thank
#!/bin/bash
if [ $# -ne 2 ] then echo 'At least two argument is required' >&2 echo "Usage: ${0##*/} file1 [file2 file3 ...]" >&2 exit 1 fi
newfile=0
for x in $* do if [ $1 -nt newfile ]; then newfile=$1 fi shift done
echo "The newest file in the list is : $newfile"
after i key in the file name and the arguments the result give me is 0. help me pls.