Created
February 18, 2015 14:04
-
-
Save rinevo/da4f485db41113714f33 to your computer and use it in GitHub Desktop.
実行ファイルとスクリプト間の引数渡しの実験
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// argtest.c | |
// 実行ファイルとスクリプト間の引数渡しの実験 | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 2) { | |
printf("%s: no input [OPTION]...\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
switch (*argv[1]) { | |
case 's': | |
printf("%s argc=%d argv=\n", argv[0], argc); | |
for (int i = 0; i < argc; i++) { | |
printf("\targv[%d]=%s\n", i, argv[i]); | |
} | |
break; | |
case 'e': | |
if (argc < 3) { | |
printf("%s: no input [OPTION] [COMMAND]...\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
char cmd[254]; | |
memset(cmd, 0, sizeof(cmd)); | |
strncat(cmd, argv[2], strlen(argv[2])); | |
for (int i = 3; i < argc; i++) { | |
strncat(cmd, " \"", 2); | |
strncat(cmd, argv[i], strlen(argv[i])); | |
strncat(cmd, "\"", 1); | |
} | |
printf("cmd=%s\n", cmd); | |
system(cmd); | |
break; | |
default: | |
printf("%s: 認識できないオプション\'%s\'です", argv[0], argv[1]); | |
printf("option:\n\ts\tshow argv\n\te\texecute argtest\n"); | |
break; | |
} | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile | |
argtest: argtest.c | |
g++ -o argtest argtest.c | |
clean: | |
rm -f argtest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script.sh | |
#!/bin/sh | |
echo "$0" | |
echo " \$0=$0" | |
echo " \$1=$1" | |
echo " \$2=$2" | |
echo " \$3=$3" | |
echo " \$4=$4" | |
echo " \$5=$5" | |
if [ "$2" = "end" ]; then | |
exit 0 | |
fi | |
./argtest $1 $2 $3 $4 | |
./argtest "$1" "$2" "$3" "$4" | |
./script.sh s end $2 $3 $4 | |
./script.sh s "end" "$2" "$3" "$4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment