# Need help converting script to binary in FreeBSD



## tangi (Mar 14, 2013)

I tried to convert shell script to binary using shc that I installed with ports.
When I execute the script, it is immediately killed.


```
$ shc -f /test.sh

$ ldd /test.x
/test.x:
	libc.so.7 => /lib/libc.so.7 (0x2808f000)

$ file /test.x
/test.x: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 8.0 (800107), stripped
```

Content of test.sh:

```
#!/bin/sh
echo "wow" >/file ;
cat /file;
```


Any help would be appreciated. Thanks


----------



## Beeblebrox (Mar 14, 2013)

1. Scripts are not binary. They exist as and are called as scripts.
2. All scripts start with the shell that is called (#!/bin/sh, #!/bin/bash, etc). so your #2 script has correct heading but your #1 script does not.
3. You execute a script by calling it directly. If you are in the directory where the script is:
`$ ./scriptname.sh`
or
`$ /path/to-the/script//scriptname.sh`
4. Change the script file permissions to executable:
`# chmod 755 scriptname.sh`


----------



## wblock@ (Mar 14, 2013)

From misc/shc:


```
A generic shell script compiler. Shc takes a script, which is specified on the
command line and produces c source code. The generated source code is then
compiled and linked to produce a stripped binary executable.

WWW: http://www.datsi.fi.upm.es/~frosal/frosal.html
```

I've never used it.


----------



## Beeblebrox (Mar 14, 2013)

@wblock: good point- I though shc was a typo for csh!

@tangi: Why would you use this? Just execute your script directly?


----------



## tangi (Mar 14, 2013)

@Beeblebrox:
depending on the tasks, binary scripts take less proc resource than source scripts because it has been compiled, optimized for the hardware. I'm not trying to obfuscate, there are some tools to easy reverse engine any binary. 

I'm curious to know how the devs has compiled their binaries. I don't have that skill for now, anyway I will be able to know if I spend a little more time developing.


----------



## tangi (Mar 14, 2013)

@wblock@: I expected that the freebsd FreeBSD forum is alive, if I compare it with the CentOS forum where no one has answered my posts.


----------



## wblock@ (Mar 14, 2013)

tangi said:
			
		

> @ Beeblebrox:
> depending on the tasks, binary scripts take less proc resource than source scripts because it has been compiled, optimized for the hardware. I'm not trying to obfuscate, there are some tools to easy reverse engine any binary.
> I'm curious to know how the devs has compiled their binaries. I don't have that skill for now, anyway I will be able to know if I spend a little more time developing.



If a shell script takes so many resources that it can benefit by being compiled, it's long past time to rewrite that script in a more capable language.


----------



## tangi (Mar 15, 2013)

wblock@ said:
			
		

> If a shell script takes so many resources that it can benefit by being compiled, it's long past time to rewrite that script in a more capable language.



Thanks for the info.
Shc on FreeBSD is working only for script using ksh interpreter.


```
freebsd# shc -v -r -f /test
shc shll=ksh
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  /test.x.c -o /test.x
shc: strip /test.x
shc: chmod go-r /test.x
```


----------

