Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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
Bash 为什么可以';t我访问从";ls";?_Bash_Variables - Fatal编程技术网

Bash 为什么可以';t我访问从";ls";?

Bash 为什么可以';t我访问从";ls";?,bash,variables,Bash,Variables,我试图搜索目录中最新的文件,我想读取文件中的第一行作为命令中的变量。当我使用下面的变量定义和附带的行调用Chromium时,我得到一个错误,即找不到文件或目录,但我们看到它被找到了,如果我在文件和行的定义之间放置一个echo$file,它将返回文件名。在错误消息中,您可以看到这一点 我尝试了许多解决方案,但除了这个错误,我什么也没有得到。对不起,如果我没有在这里全部定义它们 FILE=$(ls -t /media/presentation/Lobby2/ | head -n1) LINE=$(h

我试图搜索目录中最新的文件,我想读取文件中的第一行作为命令中的变量。当我使用下面的变量定义和附带的行调用Chromium时,我得到一个错误,即找不到文件或目录,但我们看到它被找到了,如果我在
文件
的定义之间放置一个
echo$file
,它将返回文件名。在错误消息中,您可以看到这一点

我尝试了许多解决方案,但除了这个错误,我什么也没有得到。对不起,如果我没有在这里全部定义它们

FILE=$(ls -t /media/presentation/Lobby2/ | head -n1)
LINE=$(head -n1 $FILE)
/usr/bin/chromium-browser --noerrdialogs --disable-infobars --kiosk $FILE &
我收到的错误是:

head: cannot open 'onlinepresentation.txt' for reading: No such file or directory

除非您恰好在
/media/presentation/Lobby2
head
无法找到该文件,因为
文件
仅包含
onlinepresentation.txt
——路径未列出。快速修复方法是单独保留路径:

path=/media/presentation/Lobby2
file=$(ls -t "$path" | head -n1)
line=$(head -n1 "$path/$file")
(注意一致的引用和。)

但是,不建议在脚本中使用
ls
的输出,请参阅。中讨论了获取最新的文件,一个健壮的解决方案是

for file in /media/presentation/Lobby2/*; do
    [[ $file -nt $latest ]] && latest=$file
done
line=$(head -n1 "$latest")
您甚至可以取消任何非Bash命令,并用以下命令替换
line=$(head-n1“$latest”)

IFS= read -r line < "$latest"
IFS=read-r行<“$latest”

除非您恰好在
/media/presentation/Lobby2
head
找不到文件,因为
文件
只包含
onlinepresentation.txt
——路径未列出。快速修复方法是单独保留路径:

path=/media/presentation/Lobby2
file=$(ls -t "$path" | head -n1)
line=$(head -n1 "$path/$file")
(注意一致的引用和。)

但是,不建议在脚本中使用
ls
的输出,请参阅。中讨论了获取最新的文件,一个健壮的解决方案是

for file in /media/presentation/Lobby2/*; do
    [[ $file -nt $latest ]] && latest=$file
done
line=$(head -n1 "$latest")
您甚至可以取消任何非Bash命令,并用以下命令替换
line=$(head-n1“$latest”)

IFS= read -r line < "$latest"
IFS=read-r行<“$latest”

本杰明·W.提供了一个很好的工作答案

我最终选择了:

for file in $PATH1/*.mp4; do
    [[ $file -nt $latest ]] && latest=$file
done

if [ "$latest" -nt "$PATH2/video.mp4" ]
then
    cp $latest $destination
fi

谢谢

本杰明·W.提供了一个非常有效的答案

我最终选择了:

for file in $PATH1/*.mp4; do
    [[ $file -nt $latest ]] && latest=$file
done

if [ "$latest" -nt "$PATH2/video.mp4" ]
then
    cp $latest $destination
fi
谢谢