-2

its very simple code for cmd but i cant get how its should work on my nas with shell

Please help to convert script:

@echo off
setlocal enableextensions enabledelayedexpansion

Settings

set sWorkFolder=d:\workfolder
set sSourceFolder=%sWorkFolder%\waiting-to-convert
set sDestFolder=%sWorkFolder%\done
set sBackupFolder=%sWorkFolder%\trash
set sPath2ffmpeg=%sWorkFolder%\ffmpeg\bin\ffmpeg.exe

Start cycle

for /r "%sSourceFolder%" %%i in (*.mp4) do call :action "%%i"

endlocal
exit /b

:action
setlocal enableextensions enabledelayedexpansion

set "sSourcePath=%~dp1"
set "sDestPath=%sDestFolder%!sSourcePath:%sSourceFolder%=!"
if not exist "%sDestPath%." md "%sDestPath%"
set "sDestFile=%sDestPath%%~n1.720p%~x1"

"%sPath2ffmpeg%" -i "%~f1" -vcodec h264 -vf "scale=trunc(oh*a/2)*2:min(720p\,ih)" -b:v 2000k -preset faster -acodec copy -y "%sDestFile%"

set "sBackupPath=%sBackupFolder%!sSourcePath:%sSourceFolder%=!"
if not exist "%sBackupPath%." md "%sBackupPath%"

move /y "%~1" "%sBackupPath%"

endlocal
exit /b
uran M
  • 1

1 Answers1

1

Shell scripts are normally echo off. You can get debugging output with set -vx and turn that off with set +vx. These two can be placed anywhere in the file (preferably on a line separate from others). (And as you will notice below set is very different in shell that cmd).

The first line of your script should be

#!/bin/bash

After you save your file, you need to execute the unix/linux command

chmod 755 myScript.sh

To mark it executable.

If you created file on windows, or use windows ftp, or windows anything, once you have file to *nix, do

dos2unix myScript.sh

set var=value is accomplished with DestPath=/path/to/dir. use

if [ ! -d "$DestPath" ] ; then mkdir "$DestPath'; fi

All vars are referenced like "$var".

move is shortened to mv. Consult man mv for find equivalent opt of /y.

for file in *.mp4 ; do cmd "$file" ; done

I don't understand your do :action in the for loop, so I have substituted cmd to indicate 'do some work'. You can call external commands (multiple), have an defined function that you call OR have a multi-cmd pipeline, like

cmd1 file | filter_cmd | filter2_cmd > outfile

Because you have labels in your code (i.e. :action), you should be aware that only csh allows for goto like branching (you don't want to start coding in csh!). You'll have to refactor your code to work only inside for and while loops, relying on break and continue to change processing inside the loops.

Convert this much, then use http://shellcheck.net for errors. Post a new question with your corrected code, and show exact error messages.

IHTH

shellter
  • 36,525
  • 7
  • 83
  • 90
  • `do` is part of the `for`-Syntax. `call` calls another part of the script (starting with a Label `:action`, ending with `exit /b` (here)) with `"%%i` (one of the .mp4 files) as a Parameter. This Parameter is referenced in the "subroutine" with `%1` or a [modifier](http://ss64.com/nt/syntax-args.html) of it. – Stephan Oct 30 '15 at 19:54
  • @Stephan : thanks for explaining that bit of `cmd` syntax. (Do they still call them .bat files? ). – shellter Oct 30 '15 at 22:14
  • we call the scripts "batchfile" and name them `.bat` or `.cmd`. There isn't really a common name for the "language" because it isn't a real language - just a bunch of commands and executables (we speak of scripts, not programs). Some call it "batch", some call it "cmd" and there are probably some more names for it. – Stephan Oct 31 '15 at 06:55
  • I cant get how to make this on sh: `set "sSourcePath=%~dp1" set "sDestPath=%sDestFolder%!sSourcePath:%sSourceFolder%=!"` in cycle I get Destination of source file like: `"sSourcePath=d:\workfolder\waiting-to-convert\videofolder1\video1.mp4"` then: `"sDestPath=%sDestFolder%` is:`sDestPath=d:\workfolder\done\` and `!sSourcePath:%sSourceFolder%=!` is: `\videofolder1\video1.mp4` together: `set "sDestPath=%sDestFolder%!``sSourcePath:%sSourceFolder%=!"` is: `d:\workfolder\done\videofolder1\video1.mp4` its very importan to keep all old path in Done and trash folder – uran M Nov 01 '15 at 09:45
  • stop using `%var%`. and use `"$var"` for starters. Then "Convert this much, then use http://shellcheck.net for errors. Post a new question with your corrected code, and show exact error messages." Good luck. – shellter Nov 01 '15 at 14:17