0

I've been trying since yesterday to translate a .bat file into bash, but without success because I don't know enough about the subtleties of each command, especially the Windows command call.

@echo off

echo Installing/updating bot dependencies
call npm ci --only=production --loglevel=warn >NUL

if NOT ["%errorlevel%"]==["0"] (
  pause
  exit /b %errorlevel%
)

echo Starting the bot
call npm run start

if NOT ["%errorlevel%"]==["0"] (
  pause
  exit /b %errorlevel%
)
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • I would first make the batch file better by replacing both `if` conditions by `if errorlevel 1 pause & exit /B` or use `call npm.cmd ci --only=production --loglevel=warn >NUL || (pause & exit /B)` and `call npm.cmd run start || (pause & exit /B)`. See [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) for details about the condition respectively the conditional command execution operator `||` which Linux shell interpreters support too with same meaning as the Windows command processor. `&` is just `;` in shell scripts. – Mofi Mar 22 '22 at 09:13
  • `call` is a `cmd` internal command to call a batch file from within a batch file. The Linux shell interpreters do not have such a command. So in the `.sh` file `call` needs just to be removed to run `npm` which I think is an executable on Linux and not a script file as on Windows. – Mofi Mar 22 '22 at 09:17
  • `>NUL` is coded in Linux shell scripts with `>/dev/null`. There is no command `pause` to wait for a key press in shell. There can be added a function to the `.sh` file to prompt the user for a key press although it does not really make sense to do that as Linux shell scripts are usually executed from within a terminal or an application which captures the standard and error output and displays both in a graphical window. – Mofi Mar 22 '22 at 09:20
  • 1
    The shell script could be just five lines: 1. `#!/bin/bash` (shebang to get interpreted the script file with `bash` in directory `/bin/` on running it directly and executable attribute set on `.sh` file), 2. `echo "Installing/updating bot dependencies"` 3. `npm ci --only=production --loglevel=warn >/dev/null || exit`, 4. `echo Starting the bot`, 5. `npm run start || exit`. The shebang could be also just `#!/bin/sh` to interpret the script with whatever script interpreter is the default on Linux PC. – Mofi Mar 22 '22 at 09:25
  • PS: Neither the command `pause` nor the command `exit` change the current exit code as described at [Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?](https://stackoverflow.com/questions/34968009/) So the exit code of `npm.cmd` respectively the last command/executable executed by this batch file is returned to the parent process by `cmd.exe` on using just `pause & exit /B` as both commands to not change the exit code currently assigned to dynamic variable `ERRORLEVEL`. – Mofi Mar 22 '22 at 09:30

1 Answers1

1

Here is the translation:

#!/bin/bash

echo "Installing/updating bot dependencies"

npm ci --only=production --loglevel=warn >/dev/null
exit_code=$?

if [ ${exit_code} -ne 0 ]; then
  read -p "Press ENTER to exit."
  exit ${exit_code}
fi

echo Starting the bot

npm run start
exit_code=$?

if [ ${exit_code} -ne 0 ]; then
  read -p "Press ENTER to exit."
  exit ${exit_code}
fi

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74