简体   繁体   English

如何在bash脚本中包含包含文件的目录的路径

[英]how to include path of a directory containing files inside bash script

I am writing a bash script that will get the user defined filenames and then use alsa to play that file:我正在编写一个 bash 脚本,它将获取用户定义的文件名,然后使用 alsa 播放该文件:

#!/bin/bash
export PATH=$PATH:/home/pi/Documents/audio
read -p "Enter a filename: " filename
aplay $filename

Above is what I have tried after reading about how to include path in a script.以上是我在阅读有关如何在脚本中包含路径后尝试的内容。 But at the terminal prompt, after I do ./script.sh , it returns no such a file in the directory.但是在终端提示下,在我执行./script.sh ,它在目录中没有返回这样的文件。 I also tried source ./script.sh thinking that the environment is only changed in the subshell and it returns the same.我也试过source ./script.sh认为环境只在子shell中改变并且它返回相同的。

All I need is to include the path within this script and does not change the system path permanently.我所需要的只是在此脚本中包含路径,并且不会永久更改系统路径。

I'd appreciate it if someone can point me to the right direction.如果有人能指出我正确的方向,我将不胜感激。 Thank you!谢谢!

PATH is used for finding programs, not data files. PATH用于查找程序,而不是数据文件。 If you want $filename to be a file in the audio directory, you need to concatenate it explicitly.如果您希望$filename成为audio目录中的文件,则需要明确连接它。

#!/bin/bash
audiodir=/home/pi/Documents/audio
read -r -p "Enter a filename: " filename
aplay "$audiodir/$filename"

Try specifying the absolute path of the file that you're playing like this:尝试指定您正在播放的文件的绝对路径,如下所示:

#!/bin/bash
AUDIO_PATH="/home/pi/Documents/audio"
read -p "Enter a filename: " filename
aplay "${AUDIO_PATH}/${filename}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM