【发布时间】:2014-08-12 01:30:06
【问题描述】:
我编写了 bash 脚本来打开作为参数传递的文件并将其写入另一个文件。但是只有当文件在当前目录中时,我的脚本才能正常工作。现在我还需要打开并写入不在当前目录中的文件。
如果 compile 是我的脚本的名称,那么 ./compile next/123/file.txt 应该在传递的路径中打开 file.txt。我该怎么做?
#!/bin/sh
#FIRST SCRIPT
clear
echo "-----STARTING COMPILATION-----"
#echo $1
name=$1 # 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
【问题讨论】:
-
如果你已经知道它的路径,为什么还要找到它?
-
我不是要查找文件。但是要打开它,如果它存在,并将其写入当前目录中的文件。但正如您在代码中看到的,我将 $1 复制到 name 并将 name 复制到 newfile。它没有按我的需要工作。
标签: bash shell directory arguments