views:

149

answers:

4

I have a C shell script that calls two C programs - one after the another with some file handling before, in-between and afterwards.

Now, as such I have three different files - one C shell script and 2 .c files.

I need to give this script to other users. The problem is that I have to distribute three files - which the users must keep in the same folder and then execute the script.

Is there some better way to do this?

[I know I can make one C code file out of those two... but I will still be left with a shell script and a C code. Actually, the two C codes do entirely different things... so I want them to be separate]

A: 

Use a zip or tar file? And you do realize that .c files aren't executable, you need to compile & link them first?

Keith Randall
Default zip or tar file is not an executable, so you're asking the users to extract before running
DVK
@Keith Randall: compiling and linking commands are there in the shell script.
Lazer
+2  A: 

use shar command to create self-extracting archive.

or better yet use unzipsfx with AUTORUN option.

This provides users with ONE file, and only ONE command to execute (as opposed to one for untarring and one for execution).

NOTE: The unzip command to run should use "-n" option, that way only the first run would extract the files and the subsequent would skip the extraction.

DVK
Why "or better yet use unzipsfx"? By prepending the unzip executable to the archive, its far less platform independent than shar.
jcordasc
@jcordasc - shar AFAIK doesn't allow autorun option (e.g. execute your script automatically upon unarchival)
DVK
+3  A: 

Sounds like you're worried that your users aren't savy enough to figure out how to resolve issues like command not found errors and the like. If absolutely MUST hide "complexity" of a collection of files you could have your script create the other files. In most other circumstances I would suggest that this approach is only going to increase your support workload since semi-experienced users are less likely to know how to troubleshoot the process.

If you choose to rely on the presence of a compiler on the system that you are running on you can store the C code as a collection of cat $STRING >> file.c commands to to create your two C files, which you then compile and use.

If you would want to use pre-compiled programsn instead then the same basic process can be used except instead use xxd to both generate the strings in your script and reverse the conversion process to give you working binaries. Note: Remember to chmod the binary so that it is executable.

torak
@torak: thanks!
Lazer
A: 

You can include the c code inside the shell script as a here document:

#!/bin/bash

cat > code.c << EOF
line #1
line #2
...
EOF

# compile
# execute

If you want to get fancy, you can test for the existence of the executable and skip compiling them if they exists.

If you are doing much shell programming, the rest of the Advanced Bash-Scripting Guide is worth looking at as well.

KeithB