简体   繁体   中英

Shell Script: pacmd: no command found

I am trying to write a simple shell script to detect if clementine is running. And if it is, it will turn off power saving. I tried the pacmd command in the terminal and it works perfectly, but in the script it doesn't. Afer the first error which said "pacmd no command found" I put in the full path and then it said "./noSleep.sh: line 5: [/usr/bin/pacmd: No such file or directory". So I am lost as to why it cant find it. Any help?

I don't want to turn off power savings when clementine is not playing music. So I don't want to use ps x | grep Clementine.

#!/bin/bash

while true
do
        if [/usr/bin/pacmd list-sink-inputs | grep Clementine]
    then
            xset -dpms; xset s off
    else
            xset +dpms; xset s on
    fi

    sleep 2m
done

Script Updated:

#!/bin/bash

while true
do
    if [ `echo pacmd list-sink-inputs | grep "Clementine" | wc -l` ]
    then
            xset -dpms; xset s off
            echo off
    else
            xset +dpms; xset s on
            echo on
    fi

    sleep 30s
done

It returns true everytime for some reason. I've tested Clementine not playing and playing, while testing the pacmd statement. The string Clementine shows while playing and doesn't show when not playing, so i'm not sure why it won't work.

Always use a space after [ and before ] in if statement as mentioned above in the comment by cyrus, also you are not testing anything in your if statement

grep -c "Clementine" command checks number of times "Clementine" occurs in the output of the command /usr/bin/pacmd list-sink-inputs so if it's greater than 0 it's probably open.

while true
do
    if [ "$(/usr/bin/pacmd list-sink-inputs|grep -c "Clementine")" -gt  0  ]
    then
            xset -dpms; xset s off
    else
            xset +dpms; xset s on
    fi

    sleep 2m
done

In addition to the syntax error (spaces needed around [ as pointed out), piping the results of /usr/bin/pacmd list-sink-inputs to grep Clementine causes grep to execute in a subshell . This complicates the return you are testing. The command within the subshell completed successfully, even though the result can be not found . This is where a here string , which prevents running grep in a subshell, can preserve the return you want:

#!/bin/bash

while true
do
    if [ grep -q Clementine <<<"$(/usr/bin/pacmd list-sink-inputs)" ]
    then
            xset -dpms; xset s off
    else
            xset +dpms; xset s on
    fi

    sleep 2m
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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