gpt4 book ai didi

linux - UNIX基本getopts麻烦

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:15 24 4
gpt4 key购买 nike

我最近才开始使用 UNIX,在尝试实现 getopts 时遇到了问题。

下面的函数找到一个文件,然后将其删除到回收站,虽然我正在尝试将 getopts 与 -i 一起使用,它会在文件移动后显示一条消息。语法工作正常,但当我使用 getotps 命令实现 while 循环时,它不再工作。

谁能给我一些有用的建议,我将不胜感激

function moveToBin(){

while getopts i opt
do
case $opt in
i) echo "file removed!" ;;
esac
done

if [[ -e $1 ]]; then
inode=$(ls -i $i | cut -d " " -f1)
name=$1_$inode
pathOfFile=$(pwd $1)
restoreEntry=$1_$inode:$pathOfFile/$1

mv $1 ~/deleted
mv ~/deleted/$1 ~/deleted/$name
echo "Before extension code"
extension=$(find ~ -inum $inode)
fi

最佳答案

while getopts ...; do
...
done

getopts 将解析选项及其可能的参数。它将停止解析第一个非选项参数(一个不以连字符 (-) 开头的字符串,它不是它前面任何选项的参数)。当它看到 --(双连字符)时,它也会停止解析,这意味着选项结束。

关于linux - UNIX基本getopts麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18814406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com