Reputation: 1250
I have defined this function in zsh
, but it cannot work on my computer.
function m() {
local path=/Users/james/Music/cloud_music_link
local cmd="ls $path | sed -n $1p"
echo `$cmd`
# afplay `$cmd`
}
It always said:
m:3: no such file or directory: ls /Users/james/Music/cloud_music_link | sed -n 10p
and when I copy the ls /Users/james/Music/cloud_music_link | sed -n 10p
and run it in zsh
, everything is ok, why would this happen?
And the folder cloud_music_link
is a soft link, I am not sure if this matters.
Upvotes: 1
Views: 674
Reputation: 1250
Finally I have figured it out, it is because the name of the variable path
, I changed it to p
and everything works OK. The name path
will conflict with zsh
.
This code works:
function m() {
local p=/Users/james/Music/cloud_music_link/
song="$(ls $p | sed -n $1p)"
echo $song
killall afplay
afplay "$p$song"&
}
Upvotes: 0
Reputation: 295639
You want to pass the file (or subdirectory) at a given position inside of your given directory first to echo
, and then to aplay
? Easily done, in a way compatible with both bash and zsh:
m() {
local num=${1:-1}
local dir=/Users/james/Music/cloud_music_link
local -a files=( "$dir"/* )
local file=${files[$(( num - 1 ))]}
echo "$file"
afplay "$file" &
}
See:
ls
Upvotes: 1