Shell 脚本和 md5/md5sum 命令:需要决定何时使用哪一个 [英] Shell scripts and the md5/md5sum command: need to decide when to use which one

查看:149
本文介绍了Shell 脚本和 md5/md5sum 命令:需要决定何时使用哪一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次海报,在这里,所以放轻松.:)

First time poster, here, so go easy on me. :)

很确定没有人在研究这个问题时问过这个问题.

Pretty sure nobody's yet asked this in researching this question.

简短版本:根据我在哪个机器上运行 shell 脚本,我如何告诉 shell 脚本使用一个命令与另一个命令?示例:在 Box 1 上,我想运行 md5 file.txt.在方框 3 上,我想运行 md5sum file.txt.我认为这是一个 IF 命令,如果 md5 的输出失败,请改用 md5sum .就是不知道怎么检查md5的输出是否失败

Short version: How can I tell a shell script to use one command versus the other, depending on which box I run the shell script on? Example: on Box 1, I want to run md5 file.txt. On Box 3, I want to run md5sum file.txt. I'm thining it's an IF command where if the output of md5 is a failure, use md5sum instead. Just don't know how to check and see whether the output of md5 is a failure or not

长版:我有 3 个盒子可以使用.Box 1 和 Box 3 是 Box 2 文件的接收者,当我调用 box 1/3 上的脚本时,它们会接收文件,如下所示:ftpget.sh file.txt

Long version: I have 3 boxes that I work with. Box 1 and 3 are the receivers of a file from Box 2, and they receive the file when I invoke a script on box 1/3 as follows: ftpget.sh file.txt

我有一个 shell 脚本,它执行 FTP GET 并从 Box 2 获取文件.然后它对 Box 2 的源文件和目标文件执行 md5,目标文件将在 Box 1 或 3 上,具体取决于我从哪一个执行脚本.当然,哈希值必须匹配.

I have a shell script that does an FTP GET and grabs a file from Box 2. It then does an md5 on the source file from Box 2 and the destination file, which'll be on Box 1 or 3, depending on which one I executed the script from. The hashes must match, of course.

问题在于:代码是使用md5编写的,而Box 1使用md5,Box 3使用md5sum.因此,当我从 Box 1 执行脚本时,效果很好.当我从 Box 3 执行脚本时,它失败了,因为 Box 3 使用的是 md5sum,而不是 md5.

The problem is this: The code is written to use md5, and while Box 1 uses md5, Box 3 uses md5sum. So when I execute the script from Box 1, it works great. When I execute the script from Box 3, it fails because Box 3 uses md5sum, not md5.

所以我在想:处理这个问题的最佳方法是什么?我不能安装任何东西,因为我不是管理员,而且管理这台机器的人可能不会为我做这件事.我可以在我的 .profile 中创建一个类似于:alias md5="md5sum" 的别名吗?这样,当脚本在 Box 3 上运行时,它会执行 md5 file.txt 但系统将真正执行 md5sum file.txt,因为我创建了别名.

So I was thinking: what's the best way to handle this? I can't install anything since I'm not an admin, and the people who manage the machine probably won't do it for me anyway. Could I just create an alias in my .profile which goes something like: alias md5="md5sum"? That way, when the script runs on Box 3, it'll execute md5 file.txt but the system will really execute md5sum file.txt since I created the alias.

想法?更好的想法?:)

Thoughts? Better ideas? :)

推荐答案

我不知道你使用的是什么 shell.这是用于 bash 的:

I don't know what shell you're using. This is for bash:

#!/bin/sh
md5=$(which md5)
if [ ! "${md5}" ] ; then
  md5=$(which md5sum)
  if [ ! "${md5}" ] ; then
    echo "neither md5 nor md5sum found"
    exit 1
  fi
fi
${md5} $0

这篇关于Shell 脚本和 md5/md5sum 命令:需要决定何时使用哪一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆