Re: cp output format
Hi,
On 17/07/2015 9:16 PM, Nicolas George wrote:
> Le nonidi 29 messidor, an CCXXIII, Andrew McGlashan a écrit :
>> HOME_DIR=$(grep ^${fix_name}: /etc/passwd|cut -d: -f6)
>
> "eval HOME_DIR=~$fix_name" is much simpler, more efficient, and would work
> with NIS- or LDAP-based user databases.
Will that work with ANY shell?
>> md5_1=$(md5sum $HOME_DIR/.forward|cut -d\ -f1)
>> md5_2=$(md5sum $wrk_dir/$fix_name/.forward|cut -d\ -f1)
>
> You can write "md5="${md5%% *}" instead of using cut, one fork+exec less.
Okay.
Again will that work with ANY shell?
> And you can write "md5sum < file" to have a dash instead of the file name,
> and therefore not need to remove it.
Yep.
> This is minor in this case, but in newly written code, avoid MD5, better
> use SHA-2.
>
> And of course (unless the files are large (unlikely for .forward) and on the
> same mechanical drive), cmp file1 file2 is much simpler.
Yes.
>> if [ "$md5_1" != "$md5_2" ]
>> then
>> echo "The .forward file has changed for: "$fix_name
>> diff $HOME_DIR/.forward $wrk_dir/$fix_name/.forward|sed
>> 's/^/ /'
>
>> cp -p $HOME_DIR/.forward
>> $HOME_DIR/${fix_name}.forward.$(date +%Y%m%d%H%M)
>
> You do not check for errors.
>
>> cp -p $wrk_dir/$fix_name/.forward $HOME_DIR/
>
> Dangerous: if .forward is not very small (think: handmade mailing-list), cp
> is not atomic. That means there is a small interval of time where the target
> .forward file will be only partial.
>
> To do that kind of thing reliably, you need to create a temporary file and
> rename it once it is complete. Just use rsync for the copy, it does that by
> default.
Good point.
>> fi
>> done
>> )
Thanks
A.
Reply to: