Команда: sample
Get instant Hello World example scripts, batch files and source code
for the Windows CMD.EXE shell, Linux bash, HTML code, or a simple Java/C++/PHP program as a starting point for your own editing, with the free sfk samp command for Windows, Mac OS X and Linux.
sfk sample java|cpp|... [outfile.java|.cpp|...]
print a short example code in a programming language.
supported parameters
sfk create a simple sfk script
sfkbat sfk script embedded in a .bat or .cmd
sfkbash sfk script embedded in a bash script
http automated http access example script
http -bat ... as a windows .bat file
http -bash ... as a linux .sh file
httpdata create a .zip file with webdemo files
for local use with sfk httpserv
cmd create generic windows .cmd or .bat file
bash create generic linux bash script
html simple html page with css and javascript
java create a java class doing text file I/O
javaimg create a java class doing image file I/O
javahex create a hexdump of binary data in java
javagui create a simple java gui application
cpp create a c++ text file I/O example
cppnetlog how to send UDP network text in C++
javanetlog how to send UDP network text in Java
php create command line php code for text I/O
phpimg create php example for image processing
options
-force if output file exists already, overwrite it.
command shortcut
sfk batch myscript.bat
does the same as "sfk samp sfkbat myscript.bat"
web reference
http://stahlworks.com/sfk-sample
more in the SFK Book
the SFK Book contains a 60 page tutorial, including
long sfk script examples with input, output and
detail explanations. type "sfk book" for more.
examples
sfk samp sfkbat foo.bat
creates batch file foo.bat with embedded sfk script.
type "foo.bat" to run the created script. note that
foo.bat must be created in a directory of your PATH,
or in the current directory.
sfk batch foo.bat -force
the same as above, and overwrites an existing file.
sfk batch foo.sh
create a bash script file, with lf only line endings.
can also be used under windows for mingw environments.
sfk samp java foo.java
create a java class foo. if the java JDK is available,
type "javac foo.java" and then "java foo" to run it.
sfk samp phpimg doimg.php
create image processing script that can be run by:
php doimg.php
if php.exe is in your PATH (read remarks in the script).
sfk samp javahex +toclip
copy example for java hexdump creation to the clipboard.
sfk samp http tmp.bat
create an example script for automated web/http access.
sfk samp http -bash tmp.sh
the same, but using bash in a Windows Cygwin environment.
the samples produced are:
sfk samp sfk
sfk select testfiles .txt .hpp .cpp
// find words supplied by user.
// note that %1 is the same as $1.
+find
%1 %2 %3 $4 $5 $6
// process files containing hits
+run -quiet "sfk echo \"Found hit in: [green]$file[def]\"" -yes
// run the script by:
// sfk script "thisfile" pattern1 [pattern2 ...]
sfk samp sfkbat
@echo off
sfk script "%~f0" -from begin %*
rem %~f0 is the absolute batch file name.
GOTO xend
sfk label begin -var
// default batch, with some variable support.
// for a full file backup example use:
// sfk batch -full myfile.bat
+if "%1 = " begin
+tell "[green]usage:[def]"
+tell " #(sys.ownscript.name) copy [-yes]"
+stop -all
+endif
+setvar cmd="%1"
+setvar yes="%2"
+if "#(cmd) = copy" begin
+call docopy
+stop
+endif
+tell "unknown command: #(cmd)"
+end
sfk label docopy
+copy -checkdirs
mydir mydir2
-dir core doc db
-subdir !\tmp !\save
-file !.tmp
#(yes)
+end
:xend
sfk samp sfkbash
#!/bin/bash
sfk script "$0" -from begin $@
exit
function skip_block
{
sfk label begin -var -upat2
// default batch, with unified windows/linux syntax (-upat2).
// for a full file backup example use:
// sfk batch -full myfile.sh
+if "%1 = " begin
+tell "[green]usage:[def]"
+tell " #(sys.ownscript.name) copy [-yes]"
+stop -all
+endif
+setvar cmd="%1"
+setvar yes="%2"
+if "#(cmd) = copy" begin
+call docopy
+stop
+endif
+tell "unknown command: #(cmd)"
+end
sfk label docopy
+copy -checkdirs
mydir mydir2
-dir core doc db
-subdir :\tmp :\save
-file :.tmp
#(yes)
+end
}
sfk samp http
@echo off
sfk script "%~f0" -from begin %*
rem "%~f0" is the absolute batch file name.
GOTO xend
sfk label begin -var
// the -var above enables variables everywhere.
+setvar "baseurl=http://stahlworks.com/webdemo"
+web "#(baseurl)/contents.xml"
+xex "_<category>**<id>*</id>**<name>*<
_[part4] [part8]\n_"
+perline "call listCategory #text" -yes
+end
sfk label listCategory
+echo "[green]=== List of %2: ===[def]"
+echo -spat "[yellow]Name Price[def]"
+then web "#(baseurl)/product_list_%1.xml"
// +xmlform +stop -all
+xex "=<row>**<name>*<**<price>*<
=[part4]\t[part8] \x24\n="
+filter -upat -stabform "#(-12col1) #col2"
+end
rem a longer example with input, output and detail
rem explanations is available in the SFK Book.
:xend
sfk samp java
import java.io.*;
public class fileio
{
static void log(String s) { System.out.println("main: "+s); }
public static void main(String args[]) throws Throwable
{
if (args.length < 2)
{ log("supply in- and output filename."); return; }
// copy or convert text file
BufferedReader rin = new BufferedReader(
new InputStreamReader(
new FileInputStream(args[0]), "ISO-8859-1"
// or US-ASCII,UTF-8,UTF-16BE,UTF-16LE,UTF-16
));
PrintWriter pout = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(args[1]), "ISO-8859-1"
));
while (true) {
String sline = rin.readLine();
if (sline == null) break; // EOD
log("copying line: "+sline);
pout.println(sline);
}
pout.close();
rin.close();
}
};
sfk samp cpp
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
// print error message with variable parameters.
int perr(const char *pszFormat, ...) {
va_list argList;
va_start(argList, pszFormat);
char szBuf[1024];
::vsprintf(szBuf, pszFormat, argList);
fprintf(stderr, "error: %s", szBuf);
return 0;
}
// copy text lines from one file into another.
int main(int argc, char *argv[])
{
if (argc < 2)
return 9+perr("specify input and output filename.\n");
char *pszInFile = argv[1];
char *pszOutFile = argv[2];
FILE *fin = fopen(pszInFile , "rb");
if (!fin )
return 9+perr("cannot read %s\n" , pszInFile);
FILE *fout = fopen(pszOutFile, "wb");
if (!fout)
return 9+perr("cannot write %s\n", pszOutFile);
char szBuf[1024];
memset(szBuf, 0, sizeof(szBuf));
while (fgets(szBuf, sizeof(szBuf)-10, fin))
{
char *psz = strchr(szBuf, '\r');
if (psz) *psz = '\0'; // strip cr
psz = strchr(szBuf, '\n');
if (psz) *psz = '\0'; // strip lf
printf("line: \"%s\"\n", szBuf);
strcat(szBuf, "\n");
int nlen = strlen(szBuf);
if (fwrite(szBuf, 1, nlen, fout) != nlen)
return 9+perr("failed to fully write %s\n", pszOutFile);
}
fclose(fout);
fclose(fin);
return 0;
}
sfk samp cmd
@rem windows command shell batch example
@echo off
IF "%1"=="" GOTO xerr01
echo "parameter is %1"
GOTO xdone
:xerr01
echo "please supply a parameter."
echo "example: mybat parm123"
GOTO xdone
:xdone
sfk samp bash
#!/bin/bash
function pmsg {
# uses a local variable mystr
local mystr="info: $1"
echo $mystr
}
myparm1="$1 and $2" # no blanks around "="
if [ "$2" = "" ]; then # requires all blanks
pmsg "please supply two parameters."
else
pmsg "you supplied \"$myparm1\"."
# < -lt > -gt <= -le >= -ge == -eq != -ne
i=1
while [ $i -le 5 ]; do # not "$i < 5"
echo counting: $i # quotes are optional
let i+=1 # not "i += 1" or "$i+=1"
done
fi