![組み込みシェルコマンドの終了コード [閉じる]](https://linux33.com/image/8635/%E7%B5%84%E3%81%BF%E8%BE%BC%E3%81%BF%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AE%E7%B5%82%E4%BA%86%E3%82%B3%E3%83%BC%E3%83%89%20%5B%E9%96%89%E3%81%98%E3%82%8B%5D.png)
私はカスタムシェルを作成しており、独自のシェルを使用してシェルスクリプトを実行したいと思います。 lessプログラムがインストールされていることを確認しようとしていますが、そうであればlessプログラムを使用し、そうでなければmore
。type
int do_type(int argc, const char **argv) {
bool found = false;
char *curr_path;
char *path;
char *path_strdup;
char *path_value;
char *pathValue;
pid_t pid;
pathValue = getenv("PATH");
path_strdup = strdup(pathValue);
if (path_strdup == NULL) {
perror("strdup");
exit(EXIT_FAILURE);
}
path_value = strtok(path_strdup, ":");
path = path_value;
while (path && !found) {
if ((curr_path = malloc(strlen(path) + sizeof(argv[1]))) != NULL) {
if (curr_path == NULL) {
fprintf(stderr, "malloc failed!\n");
}
strcpy(curr_path, path);
strcat(curr_path, argv[1]);
if (file_exist(curr_path)) {
found = true; // we found the program
}
//free(curr_path);
path = strtok(NULL, ":");
} else {
fprintf(stderr, "malloc failed!\n");
return false;
}
}
if (found)
printf("%s\n", curr_path, found);
else
printf("%s: not found\n", argv[1]);
return 1;
}
less
シェルでコードを実行すると、aboceコードがプログラムを見つけます(インストールされている場合)。
$ type /blaha
/blaha: not found
$ type /less
/usr/bin/less
$ type /godoc
/usr/local/go/bin/godoc
$
今確認方法を知りたいです。私のシェルが実行する必要があるスクリプトを作成しました。
type less > /dev/null
printenv|grep $1|$PAGER
私のシェルには条件ステートメントを許可する機能もあるので、if
ifステートメントで終了コード0または1を使用できる場合は、変数をより少なくまたはそれ以上に設定でき、すべてが正しく機能します。それでは、組み込み関数の終了コードは何ですか? 0は見つかった(成功)を意味し、1は見つからないことを意味し、その逆も同様です。私が考慮すべき他のことがありますか?do_type
PAGER
type
答え1
戻り値
あなたEXIT_FAILURE
のコードの対応するstdlib.h
ヘッダーファイルにEXIT_FAILURE
。EXIT_SUCCESS
#define EXIT_FAILURE 1 /* Failing exit status. */
#define EXIT_SUCCESS 0 /* Successful exit status. */
これはUNIXプログラムの一般的な動作です。 man type
ここでも便利ですtype
。POSIX コマンド:
EXIT STATUS
The following exit values shall be returned:
0 Successful completion.
>0 An error occurred.
はい。失敗した場合に1を返すのはtype
正確で予想される動作です。
EXIT_FAILURE
また、やや混乱したandの代わりにandを使用する方が良いです。EXIT_SUCCESS
return
return 1
return false
もし
if
渡されたコマンドの戻り値は、次のように評価されます。コマンドが返された場合は0
trueとして処理され、コマンドが実行されたブロックthen
以外を返すと(ブロックが存在すると仮定)、ブロックが実行されます。0
else
したがって、type
(成功した場合は0を返し、失敗した場合は1を返す)は期待どおりに機能するはずです。
if type less > /dev/null
then
echo less exists
else
echo no less we need to use more
fi
もう一つのことは、あなたのコードでこの部分が心配されることです。
if ((curr_path = malloc(strlen(path) + sizeof(argv[1]))) != NULL) {
if (curr_path == NULL) {
fprintf(stderr, "malloc failed!\n");
}
その瞬間はif
決してできません。curr_path != NULL
テスト中のみ外部ブロックに入ることができますcurr_path == NULL
。私はあなたがこれをしたいと思います:
if ((curr_path = malloc(strlen(path) + sizeof(argv[1]))) != NULL) {
... /* the actual path matching */
}
else {
fprintf(stderr, "malloc failed!\n");
}
答え2
help type
シェルからbash
:
type: type [-afptP] name [name ...] Display information about command type. For each NAME, indicate how it would be interpreted if used as a command name. ... Exit Status: Returns success if all of the NAMEs are found; fails if any are not found.