How to compile shell scripts

From Wiki-UX.info
Jump to: navigation, search

The following instructions tell you how to create C wrappper binaries for shell scripts. This technique don't actually translate a script to systems calls, it just embedded the script on a C binary, that creates a subshell process. This is not a supported HP procedure. Is provied as is without any implied warranty of fit for usage.

1. Obtain the latest HP C/aC++ compiler for your system.

The free developer trial version (90 days) can be found at:

HP C/aC++ Developer's Bundle for Evaluation

2. Install the product using standard HP-UX commands (swinstall). Verify that the component is installed.

# swlist -l fileset | grep -i ansi
# C-ANSI-C                              B.11.31.01     HP C/ANSI C Compiler
  C-ANSI-C.C                            B.11.31.01     C Programming Language
  C-ANSI-C.C-HELP                       B.11.31.01     C Online help
  C-ANSI-C.C-MAN                        B.11.31.01     C References Pages
# SWMTRANS                              A.2.0.0.591    HP-UX Software Manager (OE transition version)

3. Obtain version shc version 3.8.6. This is an opensource tools automates the "wrapping" process.

The software can be download at:

http://www.datsi.fi.upm.es/~frosal/

4. Upload the shc-3.8.6.tar.gz file to your system /var/tmp directory

5. Log to your server.

# cd /var/tmp

# /usr/contrib/bin/gunzip < shc-3.8.6.tar.gz | tar xvf -
x shc-3.8.6/CHANGES, 3549 bytes, 7 tape blocks
x shc-3.8.6/Copying, 17982 bytes, 36 tape blocks
x shc-3.8.6/Makefile, 1525 bytes, 3 tape blocks
x shc-3.8.6/match, 336 bytes, 1 tape blocks
x shc-3.8.6/pru.sh, 155 bytes, 1 tape blocks
x shc-3.8.6/shc, 39767 bytes, 78 tape blocks
x shc-3.8.6/shc.1, 3136 bytes, 7 tape blocks
x shc-3.8.6/shc.c, 24715 bytes, 49 tape blocks
x shc-3.8.6/shc.html, 4558 bytes, 9 tape blocks
x shc-3.8.6/shc.README, 1544 bytes, 4 tape blocks
x shc-3.8.6/test.bash, 138 bytes, 1 tape blocks
x shc-3.8.6/test.csh, 52 bytes, 1 tape blocks
# cd shc-3.8.6

Edit Makefile, comment in all CFLAGS entries, only left CFLAGS = -Wall -O -Ae uncommented.

6. Current version of HP-UX (11.11 - 11.31) don't support Linux ptrace library. You need to fix the code on ansi shc.c to correct the problem.

a) Comment out line 272

/*"#include <sys/ptrace.h>", */

b) Change !ptrace usage on line 301 to:

                    mine = !ptrace(10, pid, 0, 0);",

7. Compile and test shc

# make
***     ¿Do you want to probe shc with a test script?
***     Please try...   make test

# make test
***     Running a compiled test script!
***     It must show files with substring "sh" in your PATH...
        ./match.x sh
/usr/sbin/crashutil
/usr/sbin/gsscred_clean.ksh
/usr/sbin/lpshut
/usr/sbin/pushAgent
/usr/sbin/share
/usr/sbin/shareall
/usr/sbin/showmount
/usr/sbin/shutdown
/usr/sbin/smrsh
/usr/sbin/sshd
/usr/sbin/unshare
/usr/sbin/unshareall
/usr/sbin/yppush
/usr/bin/autopush
/usr/bin/c_rehash
/usr/bin/change2v9db.sh
/usr/bin/chsh
/usr/bin/csh
/usr/bin/evmshow
/usr/bin/ftpshut
/usr/bin/hash
/usr/bin/keysh
/usr/bin/ksh
/usr/bin/named-bootconf.sh
/usr/bin/remsh
/usr/bin/rksh
/usr/bin/rsh
/usr/bin/sccshelp
/usr/bin/sh
/usr/bin/shar
/usr/bin/shl
/usr/bin/showaudio
/usr/bin/showexternal
/usr/bin/shownonascii
/usr/bin/showpicture
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-keygen
/usr/bin/ssh-keyscan
/usr/contrib/bin/show_patches
/opt/perl/bin/SOAPsh.pl
/opt/perl/bin/XMLRPCsh.pl
/opt/perl/bin/instmodsh
/opt/perl/bin/ptksh
/usr/contrib/bin/X11/showrgb
/usr/contrib/bin/X11/xrefresh
/opt/dsau/bin/csshsetup
/opt/dsau/bin/dshbak
/opt/dsau/bin/pdsh
/opt/dsau/sbin/cfshow
/opt/dsau/sbin/cssh_port_monitor
/opt/perl_32/bin/SOAPsh.pl
/opt/perl_32/bin/XMLRPCsh.pl
/opt/perl_32/bin/instmodsh
/opt/perl_32/bin/ptksh
/opt/perl_64/bin/SOAPsh.pl
/opt/perl_64/bin/XMLRPCsh.pl
/opt/perl_64/bin/instmodsh
/opt/perl_64/bin/ptksh
/opt/sfm/bin/restart_sfm.sh
/opt/ssh/bin/ssh
/opt/ssh/bin/ssh-add
/opt/ssh/bin/ssh-agent
/opt/ssh/bin/ssh-keygen
/opt/ssh/bin/ssh-keyscan
/opt/mx/bin/fixmenu.sh
/opt/mx/bin/tdefshpsim.dat
/sbin/crashconf
/sbin/kcshutdown
/sbin/krs_flush
/sbin/savecrash
/sbin/sh
/sbin/shutdown
[14726] PAUSED... Hit return!

***     ¿Do you want to see strings in the generated binary?
***     Please try...   make strings

# make strings
***     Running: "strings -n 5 match.x"
***     It must show no sensible information...
        strings -n 5 match.x
<null>
M|&4g
2I,<A
{rH7@
e<G|CX
T?g;>
3&"p>
*tRgex
2|hQ?
%x@sC
***     ¿Do you want to install shc?
***     Please try...   make install

8. The shc program is ready for usage.

a) Create a test shell script, for example vg00.sh:

#!/usr/bin/sh
export PATH=/usr/sbin:/usr/bin
vgdisplay /dev/vg00

c) Check that the script produces the correct result:

# chmod +x vg00.sh

# ./vg00.sh
--- Volume groups ---
VG Name                     /dev/vg00
VG Write Access             read/write
VG Status                   available
Max LV                      255
Cur LV                      9
Open LV                     9
Max PV                      16
Cur PV                      2
Act PV                      2
Max PE per PV               4350
VGDA                        4
PE Size (Mbytes)            8
Total PE                    8682
Alloc PE                    6810
Free PE                     1872
Total PVG                   0
Total Spare PVs             0
Total Spare PVs in use      0
VG Version                  1.0.0

d) Compile the script. It will produce vg00.sh.x binary file.

# ./shc -f vg00.sh

e) Test and execute the script

# chmod 0111 vg00.sh.x

# ll -l vg00.sh.x
---x--x--x   1 root       sys          24576 Jan 15 11:03 vg00.sh.x

# what vg00.sh.x
vg00.sh.x:
        $Revision: 92453-07 linker linker crt0.o B.11.58 061104 $

# ./vg00.sh.x
--- Volume groups ---
VG Name                     /dev/vg00
VG Write Access             read/write
VG Status                   available
Max LV                      255
Cur LV                      9
Open LV                     9
Max PV                      16
Cur PV                      2
Act PV                      2
Max PE per PV               4350
VGDA                        4
PE Size (Mbytes)            8
Total PE                    8682
Alloc PE                    6810
Free PE                     1872
Total PVG                   0
Total Spare PVs             0
Total Spare PVs in use      0
VG Version                  1.0.0