in reply to RFC: UNIX shell to Perl converter

Well, the most trivial step that would do what you ask was done by me as an April Fool's Joke about a decade ago: the sh2perl translator. However, it's basically a punt, because it puts the entire original script inside a call to system.

Doing anything more really doesn't gain you much, because the shell doesn't do much, and you wouldn't be able to eliminate a single fork. So you'd be replacing one launcher with another. Why bother?

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: RFC: UNIX shell to Perl converter
by cdarke (Prior) on Oct 09, 2006 at 17:31 UTC
    With respect, the shell can do quite a lot. Korn shell and Bash are more than just program launchers, Bash 3, for example, supports Extended REs. Whole applications are written in Korn, however mis-guided that might be.
    I agree that converting a shell script that just called external programs to a perl script that called the same external programs would be a waste of time, but I hope to replace utility programs with Perl equivalents.
      No, you can't really eliminate any forks, because you don't know who needs to know $? at the right time, or depends on a certain number of kids being present.

      It's like any translation problem... there's the surface syntax, which might appear relatively easy to translate, but then there's the deep semantics: all the side effects of the steps. So you either have to emulate the deep semantics precisely, or you have to analyze and understand enough of the rest of the program to know what you can avoid emulating. Ugh, on either side.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Indeed. $? is set by just about any command in the shell (I'm not talking csh here), including [, [[, and ((, so it can affect the entire script, not just forks. Handling this is central to the task.
Re^2: RFC: UNIX shell to Perl converter
by Anonymous Monk on Mar 11, 2015 at 04:58 UTC
    #!/bin/ksh ####### Error Log Script ############################################# +#################### ###################################################################### +##################### function find { flag=0 if [ ! -d ~/logs ]; then mkdir ~/logs fi date=`date +%Y-%m-%d-%s` while true do grep -ni "$keyword" $source | while read line do if [ "$(ls -A ~/logs 2> /dev/null)" == "" ]; t +hen echo $line >> ~/logs/sanity-$date.log flag=1 else result=`cat ~/logs/* | grep -F "$line" +` if [ "$result" != "$line" ]; then echo $line >> ~/logs/sanity-$d +ate.log flag=1 fi fi done done if [ $flag == 1 ]; then echo "######################################## +#############################################" echo "Errors or Exception found. Extraction is + copied in the file ~/logs/sanity-$date.log" echo "######################################## +#############################################" else echo "###################" echo "No new logs Found" echo "###################" fi } action=$1 #Start or Stop dir=$2 file=$3 #Source filename keyword=$4 #Search String case $action in start) if [ "$dir" == "" ]; then echo "Enter the path" exit fi if [ ! -d $dir ]; then echo "Directory not found" exit fi if [ "$file" == "" ]; then echo "Enter the filename" exit fi if [ ! -f $dir/$file ]; then echo "No File found" exit fi source=$dir/$file if [ "$keyword" == "" ]; then echo "Enter a keyword for search" exit fi find exit ;; stop) echo "killed" exit ;; *) echo '######################################## +##############################' echo 'Usage: sh parser.sh [start/stop] [Direct +ory path] [Filename] [Keyword]' echo 'Sample: sh parser.sh start /var/log/ mes +sages "test"' echo '######################################## +##############################' exit ;; esac