bash 将路径作为参数传递给 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24341686/
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-18 10:43:55  来源:igfitidea点击:

Passing a path as an argument to a shell script

bashshelldirectoryarguments

提问by Hymanzz

I've written bash script to open a file passed as an argument and write it into another file. But my script will work properly only if the file is in the current directory. Now I need to open and write the file that is not in the current directory also.

我已经编写了 bash 脚本来打开作为参数传递的文件并将其写入另一个文件。但是只有当文件在当前目录中时,我的脚本才能正常工作。现在我还需要打开并写入不在当前目录中的文件。

If compile is the name of my script, then ./compile next/123/file.txtshould open the file.txtin the passed path. How can I do it?

如果 compile 是我的脚本的名称,那么./compile next/123/file.txt应该file.txt在传递的路径中打开。我该怎么做?

#!/bin/sh
#FIRST SCRIPT
clear
echo "-----STARTING COMPILATION-----"
#echo 
name=   # Copy the filename to name
find . -iname $name -maxdepth 1 -exec cp {} $name \;
new_file="tempwithfile.adb"
cp $name $new_file #copy the file to new_file

echo "compiling"
dir >filelist.txt
gcc writefile.c
run_file="run_file.txt"
echo $name > $run_file
./a.out
echo ""
echo "cleaning"
echo ""

make clean
make -f makefile
./semantizer -da <withfile.adb

采纳答案by polym

Your code and your question are a bit messy and unclear.

您的代码和您的问题有点混乱和不清楚。

It seems that you intended to findyour file, given as a parameter to your script, but failed due to the maxdepth.

您似乎打算将find文件作为脚本的参数提供,但由于maxdepth.

If you are given next/123/file.txtas an argument, your findgives you a warning:

如果你被next/123/file.txt当作一个论点,你find会给你一个警告:

find: warning: you have specified the -maxdepth option after a non-option argument -iname, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

find: 警告:您在非选项参数 -iname 之后指定了 -maxdepth 选项,但选项不是位置性的(-maxdepth 影响在它之前指定的测试以及在它之后指定的测试)。请在其他参数之前指定选项。

Also -maxdepthgives you the depth findwill go to find your file until it quits. next/123/file.txthas a depth of 2directories.

-maxdepth为您提供了深度find会去找到你的文件,直到它退出。next/123/file.txt具有2目录深度。

Also you are trying to copy the given file within find, but also copied it using cpafterwards.

此外,您正在尝试在 中复制给定的文件find,但也使用cp之后复制它。

As said, your code is really messy and I don't know what you are trying to do. I will gladly help, if you could elaborate :).

如上所述,您的代码真的很乱,我不知道您要做什么。如果您能详细说明,我很乐意提供帮助:)。

There are some questions that are open:

有一些问题是开放的:

  1. Why do you have to findthe file, if you already know its path? Do you always have the whole path given as an argument? Or only part of the path? Only the basename?
  2. Do you simply want to copy a file to another location?
  3. What does your writefile.cdo? Does it write the content of your file to another? cpdoes that already.
  1. find如果您已经知道文件的路径,为什么还要访问该文件?您是否总是将整个路径作为参数给出?还是只是路径的一部分?只有basename?
  2. 您只是想将文件复制到另一个位置吗?
  3. 你是writefile.c做什么的?它是否将您的文件内容写入另一个?cp已经这样做了。

I also recommend using variables with CAPITALIZED letters and checking the exit status of used commands like cpand find, to check if these failed.

我还建议使用带有 CAPITALIZED 字母的变量并检查所用命令的退出状态,例如cpfind,以检查这些命令是否失败。

Anyway, here is my script that might help you:

无论如何,这是我的脚本可能会对您有所帮助:

#!/bin/sh
#FIRST SCRIPT
clear
echo "-----STARTING COMPILATION-----"
echo "FILE: "
[ $# -ne 1 ] && echo "Usage: ##代码## <file>" 1>&2 && exit 1

FILE=""   # Copy the filename to name
FILE_NEW="tempwithfile.adb"
cp "$FILE" "$FILE_NEW" # Copy the file to new_file
[ $? -ne 0 ] && exit 2

echo
echo "----[ COMPILING ]----"
echo
dir &> filelist.txt # list directory contents and write to filelist.txt
gcc writefile.c # ???

FILE_RUN="run_file.txt"
echo "$FILE" > "$FILE_RUN"

./a.out

echo
echo "----[ CLEANING ]----"
echo

make clean
make -f makefile
./semantizer -da < withfile.adb