得到“找不到命令". bash脚本中的错误 [英] Getting "command not found" error in bash script

查看:105
本文介绍了得到“找不到命令". bash脚本中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个bash脚本,该脚本根据输入参数获取三个路径,然后在该路径中获取图像名/文件名.

I have written a bash script that gets three paths based on input parameters and then then gets the imagename/filename in the path.

类似的东西:
我提供:

Something like:
I provide:

AA=/home/user

然后使用find命令获取
/home/user/dir2/images/dir/tellmeimage1fun.bin

Then it uses the find command to get
/home/user/dir2/images/dir/tellmeimage1fun.bin

最后,我必须将tellmeimage1fun.bin作为输出.

Finally I have to get tellmeimage1fun.bin as output.

脚本:

#!/bin/bash  

echo "arg0 n/k/d"  

AA=$1  
CC=$3  

PATH1="`find $AA/dir2/images/dir/ -name *image1*.bin`"  
PATH2="`find $AA/dir2/images/dir/ -name *bimage2*.bin`"  
PATH3="`find $AA/dir2/images/dir/ -name *cimage3*.bin`"  

if [ $CC = "n" ] ; then  
    PATH=$PATH1  
elif [ $CC = "k" ] ; then  
    PATH=$PATH2  
else  
    PATH=$PATH3  
fi  

#Getting filename name from path:  
IMG="`ls $PATH | cut -d "/" -f6`"

OUTPUT:  
/users/prasapat/bin/sl5: line 22: ls: command not found  
/users/prasapat/bin/sl5: line 22: cut: command not found  

如果我给出了ls的完整路径并削减了它们的工作范围.但是我不想对脚本中的所有命令执行此操作.如果我删除最后一行并回显PATH变量,那是完全可以的.仅在添加了最后一条命令后,我才看到问题所在.

If I give complete paths to ls and cut they work. But i don't want to do that for all commands in the script. If i remove the last line and echo the PATH variable it is completely fine. Only after adding the last command, I see the problem.

我做错了什么?

推荐答案

问题是,如果调用时未使用完整路径,则您将重新定义PATH变量,bash将在其中查找二进制文件.

The problem is that you are redefining the PATH variable where bash looks into to find the binary files if you don't use a complete path when calling.

您应该将bash脚本中的PATH更改为MYPATH或类似的名称,以免与已经存在的环境变量混淆.

You should change the PATH in your bash script to MYPATH or something like that, so that it doesn't mess with the already environmental variables.

如果您不知道PATH变量是什么,可以查看维基百科的文章

If you don't know what the PATH variable is for you can look at wikipedia's article

这篇关于得到“找不到命令". bash脚本中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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