0

Migrating kornshell scripts from solaris 10 to RHEL 7.5

/bin/sh is used to execute shell scripts in RHEL 7.5

$ ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Jul 16 12:10 /bin/sh -> bash

So, we would like to first parse the complete code to verify their syntax but not execute it,

For example:

sleep 20
echo test
cd /a/b/c
connect # instead of ./connect

should just parse the code but not execute it

What is the approach to just parse the code? to verify syntax without executing it.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 1
    sourcing the code = executing it. You're looking for a static analysis tool. Check out https://github.com/koalaman/shellcheck –  Nov 15 '18 at 01:41
  • @swalladge shell check is too much of headache... giving syntax error for every line. It actually does not give error for line 4 – overexchange Nov 15 '18 at 01:43
  • 2
    Just did a quick search - it seems that most shells have a `-n` flag to parse but not execute the code (exactly what you were asking for). However note that this won't catch the bug on line 4 since that is still correct syntax. –  Nov 15 '18 at 01:47
  • @swalladge shall I say `./shell_script.sh -n` ? – overexchange Nov 15 '18 at 01:49
  • No, it would be `sh -n shell_script.sh`. The `-n` flag needs to be passed to the shell executable, rather than the script. –  Nov 15 '18 at 01:51
  • @swalladge Does `-n` not take care of `command not found` errors? until script execute... – overexchange Nov 15 '18 at 01:54
  • Finding the command to run is part of execution. So if you don't execute the command, you don't know whether it would be found. That's the way the shell works. – rici Nov 15 '18 at 02:03
  • 3
    It's entirely possible that a given command executable is created by the previous command (eg. by compiling something). So it would be incorrect to reject the script because a command doesn't exist before the script is executed. – rici Nov 15 '18 at 02:05
  • 2
    Might be a stupid question, but why migrating from ksh to sh ? why not just use ksh on RHEL ? Just curious. – Andre Gelinas Nov 15 '18 at 03:44

0 Answers0