Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本卡住了_Linux_Shell - Fatal编程技术网

Linux Shell脚本卡住了

Linux Shell脚本卡住了,linux,shell,Linux,Shell,我有一个大学课程要完成,我被困在这一部分: del-此脚本应将调用的文件移动到dustbin目录,以便在以后必要时将文件还原到其原始位置 我已尝试如下所示,但不起作用: #!/bin/bash echo "Do you want to delete this file?" echo "Y/N" read ans case "$ans" in Y) echo "`readlink -f $1`" >>/TAM/store & mv $1 /~/dustbin ;; N)

我有一个大学课程要完成,我被困在这一部分:

del-此脚本应将调用的文件移动到dustbin目录,以便在以后必要时将文件还原到其原始位置

我已尝试如下所示,但不起作用:

#!/bin/bash
echo "Do you want to delete this file?"
echo "Y/N"
read ans
case "$ans" in
  Y) echo "`readlink -f $1`" >>/TAM/store & mv $1 /~/dustbin ;;
  N) echo "File not deleted" ;;
esac
当我运行它时,我得到以下信息:

./Del: line 8: /TAM/store: No such file or directory
MV: missign destination file operand after '/~/dustbin'
另外,如何使用用户输入输入文件名?或者你不能那样做


p.S.
~
是根目录,
TAM
是我的目录,
store
是文件,
dustbin
根目录中的
dustbin
目录
Del
是脚本的名称

因为你说这是课程工作,所以我不会给你一个完整的解决方案,而是一个非常简单(简化)的开始:

这应该让您开始解决“要么接受参数输入,要么如果没有,允许用户输入文件名”的问题

如果您通过了检查,您就知道filename包含有效的文件名,因此您可以删除
readlink
调用(尽管它不会给出完整的路径),但您可以使用
printf“$filename”>>DEST


bash
手册中有很多好的信息可以阅读。(试试看:
manbash

你还没有告诉我们什么不起作用。我想说的是,很有可能会找到一个“更好”的解决方案,但你的意思可能是
&
而不是
&
。关于第二个问题,如果可以让用户输入文件名;提示:您如何阅读用户的答案?也许可以用来读取文件名?;)第三,
$1
包含程序的第一个(如果有)参数。请确保
$1
不是空的。它似乎是空的。感谢Jite解决了&问题,排除了一个错误,我将接受您所说的其他内容并尝试进行一些编辑。请注意,
~
不是
根目录,而是您的主目录,shell将
~
扩展到
/home/TAM/
根目录是
/
,谢谢你,我会努力让它工作并阅读手册。我更改了关于
readlink
的答案,尽管我从未使用过它,但似乎我测试的方式不对。我已经编辑了关于那个部分的答案。只需注意,
$#
是程序的参数数量,如
/del arg1 arg2 arg3
$#设置为3
$1等于arg1
等。
#!/bin/sh

if [ $# -eq 0 ]; then
    printf "You didn't give an argument, please input file name: \n"
    filename=READ_FILE_NAME_HERE
elif [$# -eq 1 ]; then
    filename=$1
else
    printf "Error: You gave to many parameters!\n"
    exit 1
fi

# Does the file exist (and is a regular file)?
[ -f "$filename" ] || {
    printf "Error: File doesn't exist or isn't a regular file.\n"
    exit 2
    }

Do_you_really_want_to_delete_the_file?
Do_the_remove_magic