【问题标题】:shell scripting - file existenceshell 脚本 - 文件存在
【发布时间】:2017-03-12 15:42:02
【问题描述】:

我想问一个关于 shell 脚本的问题 假设有两个路径可以安装文件 Path_1Path_2

脚本首先应该在Path_1 上查找文件 如果文件不存在或未安装在Path_1 那么脚本应该在path_2 上查找文件 如果它也不在那里,则显示 error

我有这个 代码:-# Make sure that sh.exe is found in C:\Program Files (x86)\Git\bin\sh.exe ls /c/Program\ Files\ \(x86\)/Git/bin/sh.exe > /dev/null 2>&1 || { echo echo -e "\t Git Bash was not installed in the default location." echo -e '\t Installation requires sh.exe to be found at C:\Program Files (x86)\Git\\bin\sh.exe.' echo echo -e "\e[0;31mAborting...\e[m" waitForEnterAndExit } echo -e "\t Git Bash installation found in C:\Program Files (x86)/Git/bin/sh.exe"

现在sh.exe可以安装在

C:\Program Files\Git\bin

 C:\Program Files (x86)\Git\bin\sh.exe

现在我想更改脚本,以便它首先在

处检查文件

C:\Program Files\Git\bin

如果文件不存在则在

 C:\Program Files (x86)\Git\bin\sh.exe

【问题讨论】:

  • 你自己做了什么来找到答案的?
  • 尝试谷歌搜索"bash file exists"

标签: linux shell terminal


【解决方案1】:

这是您可以部署的简单示例代码,请务必修改 Path_1 和 Path_2 的值。

#!/bin/bash                                                                                                                                                                                                  

Path_1="path1"
Path_2="path2"

Filename=$1

[ -z ${Filename} ] && echo Filename cannot be empty && exit -1

[ -e ${Path_1}/${Filename} ] && echo Found ${Filename} under ${Path_1} && exit 0
[ -e ${Path_2}/${Filename} ] && echo Found ${Filename} under ${Path_2} && exit 0

echo ${Filename} cannot be found under ${Path_1} and ${Path_2} && exit -1

【讨论】:

  • 我也需要更改文件名的值。对吧??
  • 不需要修改脚本中的文件名。文件名是脚本的第一个参数,用法:./script Filename
猜你喜欢
  • 2021-08-22
  • 2018-05-19
  • 2016-02-19
  • 2013-03-13
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
相关资源
最近更新 更多