How to execute HandbrakeCLI from script? - macos

I'm trying to execute a script that runs a Handbrake video conversion.
If I run the following command from the terminal, it works fine:
HandbrakeCLI -i inputtestfile.mp4 -o outputtestfile.mp4 -z "Preset"
If I put this into a .sh script (and give the script execution rights using chmod a+x), I get the following error:
HandbrakeCLI: Command not found
If I then put the full path to HandbrakeCLI, it then works, for example:
/usr/local/Cellar/handbrake/1.2.0/bin/HandBrakeCLI -i inputtestfile.mp4 -o outputtestfile.mp4 -z "Preset"
However, the above method is inconvenient because every time I update Handbrake to a new version, I'll have to update the script.
How can I add Path (I think this is right), to the script, so I can just use:
HandbrakeCLI -i inputtestfile.mp4 -o outputtestfile.mp4 -z "Preset"
Thanks

You can begin the .sh script with:
PATH=$PATH:/usr/local/Cellar/handbrake/1.2.0/bin
path is an environment variable that affects where sh would look unqualified files names (commands) from.
And for the changing version you can parse HandBrake --version output and use it as a variable instead of 1.2.0 string in PATH.

Related

command substitution not working in alias?

I wanted to make an alias for launching a vim session with all the c/header/makefiles, etc loaded into the buffer.
shopt -s extglob
alias vimc="files=$(ls -A *.?(c|h|mk|[1-9]) .gitconfig [mM]akefile 2>/dev/null); [[ -z $files ]] || vim $files"
When I run the command enclosed within the quotations from the shell, it works but when run as the alias itself, it does not. Running vimc, causes vim to launch only in the first matched file(which happens to be the Makefile) and the other files(names) are executed as commands for some reason(of course unsuccessfully). I tried fiddling around and it seems that the command substitution introduces the problem. Because running only the ls produces expected output.
I cannot use xargs with vim because it breaks the terminal display.
Can anyone explain what might be causing this ?
Here is some output:
$ ls
Makefile readme main.1 main.c header.h config.mk
$ vimc
main.1: command not found
main.c: command not found
.gitignore: command not found
header.h: command not found
config.mk: command not found
On an related note, would it be possible to do what I intend to do above in a "single line", i.e without storing it into a variable files and checking to see if it is empty, using only the output stream from ls?

How to avoid changing my bash command use in all my scipts

So i have a bash command that has the following options
-v -o -T -S -I -e -t
-t has been changed to -x and -T and -e are no longer availabe.
How can i avoid changing all scripts that use this command with these options that are no longer available or have changed?
You can create a wrapper for your bash command and put it in the PATH before the other executable that is changing.
For instance:
Imaging that the changing bash command is in the /c directory and this is your PATH:
PATH=/a:/b:/c
One approach is to put the wrapper with the same name in the /a (or /b) directory - that is, in the PATH before /c. So, let's say your old script is called old and it's in the /c directory. You can create an old script in the /a directory, and have it call the other script:
COMMAND="/c/old $( sed -e "s:-x::g" -e "s:-T::g" <<< "$#" )"
$COMMAND
So the idea is to manipulate the command arguments before calling the /c/old script. This will need a bit of adjusting if the parameters are more complicated (like they can take a value). There is also likely a quoting issue, it is unlikely that quotes will survive this approach.
If you need to get more complicated, you may consider getopts as a way of parsing the parameters better in the /a/old script.
To be honest, I'm not so happy with this answer - it will not work in a general case. But you asked :) ...

Bash check if script is running with exact options

I know how to check if a script is already running (if pidof -o %PPID -x "scriptname.sh"; then...). But now I have a script that accepts inputs as flags, so it can be used in several different scenarios, many of which will probably run at the same time.
Example:
/opt/scripts/backup/tar.sh -d /directory1 -b /backup/dir -c /config/dir
and
/opt/scripts/backup/tar.sh -d /directory2 -b /backup/dir -c /config/dir
The above runs a backup script that I wrote, and the flags are the parameters for the script: the directory being backed up, the backup location, and the configuration location. The above example are two different backups (directory 1 and directory 2) and therefore should be allowed to run simultaneously.
Is there any way for a script to check if it is being run and check if the running version is using the exact same parameters/flags?
The ps -Af command will provide you all the processes that run on you os with the "command" line used to run them.
One solution :
if ps auxwww | grep '/[o]pt/scripts/backup/tar.*/directory2'; then
echo "running"
else
echo "NOT running"
fi

shell scripting no such file or directory

I wrote a shell script that calls the ffmpeg tool but when I run it, it says No such file or directory yet it does!
Here is my script:
#!/bin/bash
MAIN_DIR="/media/sf_data/pipeline"
FFMPEG_DIR="/media/sf_data/livraison_transcripts/ffmpeg-git-20180208-64bit-static"
for file in MAIN_DIR/audio_mp3/*.mp3;
do
cp -p file FFMPEG_DIR;
done
for file in FFMPEG_DIR/*.mp3;
do
./ffmpeg -i ${file%.mp3}.ogg
sox $file -t raw --channels=1 --bits=16 --rate=16000 --encoding=signed-
integer --endian=little ${file%.ogg}.raw;
done
for file in FFMPEG_DIR/*.raw;
do
cp -p file MAIN_DIR/pipeline/audio_raw/;
done
and here is the debug response:
cp: cannot stat ‘file’: No such file or directory
./essai.sh: line 14: ./ffmpeg: No such file or directory
sox FAIL formats: can't open input file `FFMPEG_DIR/*.mp3': No such file or
directory
cp: cannot stat ‘file’: No such file or directory
FYI I'm running CentOS7 on VirtualBox
Thank you
Here's a Minimal, Complete, and Verifiable example (MCVE), a version of your script that removes everything not required to show the problem:
#!/bin/bash
MAIN_DIR="/media/sf_data/pipeline"
echo MAIN_DIR
Expected output:
/media/sf_data/pipeline
Actual output:
MAIN_DIR
This is because bash requires a $ when expanding variables:
#!/bin/bash
MAIN_DIR="/media/sf_data/pipeline"
echo "$MAIN_DIR"
The quotes are not required to fix the issue, but prevent issues with whitespaces.
Hi You need couple of correction in your shell script see below. To get the actual value assigned to a variable you need to add $ at the front of the variable in shell script.
for file in $"MAIN_DIR"/audio_mp3/*.mp3;
do
cp -p "$file" "$FFMPEG_DIR";
done
for file in "$FFMPEG_DIR"/*.mp3;
./ffmpeg -i ${file%.mp3}.ogg
#provide full path like /usr/bin/ffmpeg
for file in "$FFMPEG_DIR"/*.raw;
do
cp -p "$file" "$MAIN_DIR"/pipeline/audio_raw/;
done

how do I find where a executable is present in macosx?

I have a command called youtube-dl .. but dont know where it is installed.. i can run it from shell.. how do i find where it is installed ? which youtube-dl doesnt say anything..
Bash has a command that will show whether a command is an alias, a function or an executable in your path (and, if so, where):
type -a youtube-dl
It's much better than which which doesn't include aliases or functions.
If you can't find it with which (or whereis) then it could be:
a function defined in .bashrc or .profile (or some other file the shell loads on startup or login)
an alias defined in one of the above files.
You can search your environment for youtube-dl:
$ set | grep youtube-dl
or save it to some file and load it into a texteditor:
$ set >myenv
$ open -a textedit myenv
and for the aliases:
$ alias >myalias
or
$ alias | grep youtube-dl
Have you tried
whereis youtube-dl
?
Otherwise you could just search for it:
find / -name youtube-dl

Resources