在指定文件夹中运行脚本的快速 bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4691500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:16:02  来源:igfitidea点击:

Quick bash script to run a script in a specified folder?

bashubuntu

提问by skyeagle

I am attempting to write a bash script that changes directory and then runs an existing script in the new working directory.

我正在尝试编写一个更改目录的 bash 脚本,然后在新工作目录中运行现有脚本。

This is what I have so far:

这是我到目前为止:

#!/bin/bash
cd /path/to/a/folder
./scriptname

scriptname is an executable file that exists in /path/to/a/folder - and (needless to say), I do have permission to run that script.

scriptname 是一个存在于 /path/to/a/folder 中的可执行文件 - 而且(不用说),我确实有权运行该脚本。

However, when I run this mind numbingly simple script (above), I get the response:

然而,当我运行这个头脑简单的脚本(上面)时,我得到了回应:

scriptname: No such file or directory

脚本名:没有那个文件或目录

What am I missing?! the commands work as expected when entered at the CLI, so I am at a loss to explain the error message. How do I fix this?

我缺什么?!这些命令在 CLI 中输入时按预期工作,因此我无法解释错误消息。我该如何解决?

回答by gabuzo

Looking at your script makes me think that the script you want to launch a script which is locate in the initial directory. Since you change you directory before executing it won't work.

查看您的脚本让我认为您要启动位于初始目录中的脚本的脚本。由于您在执行之前更改了目录,因此它不起作用。

I suggest the following modified script:

我建议使用以下修改后的脚本:

#!/bin/bash
SCRIPT_DIR=$PWD
cd /path/to/a/folder
$SCRIPT_DIR/scriptname

回答by Satya

cd /path/to/a/folder
pwd
ls
./scriptname

which'll show you what it thinks it's doing.

这会告诉你它认为它在做什么。

回答by thkala

I usually have something like this in my useful script directory:

我通常在我有用的脚本目录中有这样的东西:

#!/bin/bash

# Provide usage information if not arguments were supplied
if [[ "$#" -le 0 ]]; then
        echo "Usage: 
$ cdexec
Usage: /home/archon/bin/cdexec <executable> [<argument>...]
$ cdexec /bin/ls ls
ls
$ cdexec /bin/xxx/ls ls
Directory '/bin/xxx/' does not exist
$ cdexec /ls ls
Executable 'ls' does not exist in '/'
<executable> [<argument>...]" >&2 exit 1 fi # Get the executable by removing the last slash and anything before it X="${1##*/}" # Get the directory by removing the executable name D="${1%$X}" # Check if the directory exists if [[ -d "$D" ]]; then # If it does, cd into it cd "$D" else if [[ "$D" ]]; then # Complain if a directory was specified, but does not exist echo "Directory '$D' does not exist" >&2 exit 1 fi fi # Check if the executable is, well, executable if [[ -x "$X" ]]; then # Run the executable in its directory with the supplied arguments exec ./"$X" "${@:2}" else # Complain if the executable is not a valid echo "Executable '$X' does not exist in '$D'" >&2 exit 1 fi

Usage:

用法:

#!/bin/bash
/path/to/a/folder/scriptname

回答by Paused until further notice.

One source of such error messages under those conditions is a broken symlink.

在这些情况下,此类错误消息的一个来源是损坏的符号链接。

However, you say the script works when run from the command line. I would also check to see whether the directory is a symlink that's doing something other than what you expect.

但是,您说脚本在从命令行运行时有效。我还会检查该目录是否是一个符号链接,它的作用与您期望的不同。

Does it work if you call it in your script with the full path instead of using cd?

如果您在脚本中使用完整路径而不是使用 cd 调用它,它是否有效?

##代码##

What about when called that way from the command line?

从命令行以这种方式调用时呢?