C程序调用shell脚本 [英] C program calling shell script

查看:123
本文介绍了C程序调用shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的C程序调用shell脚本myScript.sh。我得到RET的价值256请帮我了解了什么错误的系统调用?

I have a small C program calling a shell script myScript.sh. I am getting the value of ret as 256. Please help me in knowing what went wrong with the system call?

int main()
{
int ret;
ret = system (myScript.sh);
ret >>=  ret;
if (ret != 0)
{
  printf("ret is [%d]",ret);
}
}

工作在64位UNIX操作系统,并使用KSH的Shell

Working on 64 bit UNIX operating system and using ksh shell

推荐答案

系统函数通常工作在* nix的方式是,它调用键,那么孩子调用与 / bin / sh的 EXEC 功能之一 -c ,然后你传递给系统中的孩子,果然子进程进入的一个实例的字符串它运行命令 / bin / sh的程序。父称的的功能之一,它等待 / bin / sh的退出,它与不同样退出状态的shell脚本,然后系统还返回该值。

The way that the system function usually works on *nix is that it calls fork and then the child calls one of the exec functions with /bin/sh -c and then the string you passed to system in the child, which turns the child process into an instance of the /bin/sh program which runs the command. The parent calls one of the wait functions, which waits for the /bin/sh to exit, which it does with the same exit status as the shell script, and then system also returns that value.

如果你看一下手册页系统调用(S):

If you look at the man pages for the wait system call(s):

main 3 wait

您应该得到什么回报得到一些宏观功能,帮助您理解它的一些信息。

You should get some information about what gets returns and some macro functions that help you make sense of it.

WIFEXITED(stat_val)宏可用于测试是否程序正常退出与信号反对。正常退出涉及调用退出系统调用。如果这个函数返回一个非零值,那么你可以使用 WEXITSTATUS(stat_val)宏来得到它实际返回的值。

The WIFEXITED(stat_val) macro can be used to test if the program exited normally as opposed to with a signal. Normal exits involve calling the exit system call. If this function returns a non-zero value then you can use the WEXITSTATUS(stat_val) macro to get the value that it actually returned.

WIFSIGNALED(stat_val)宏可用于测试是否该程序是用信号终止,如果是这样的 WTERMSIG(stat_val) 宏将返回导致终止的信号编号。

The WIFSIGNALED(stat_val) macro can be used to test if the program was terminated with a signal, and if so the WTERMSIG(stat_val) macro will return the signal number that caused the termination.

有一些其他的宏,它可以告诉你,如果过程中被停止或继续,而不是终止,但我不认为他们过于对您有所帮助用于此目的,但你可能想看看他们。

There are some other macros that can tell you if the process were stopped or continued, rather than terminated, but I don't think that they are overly helpful to you for this purpose, but you may want to look into them.

至于什么在这种情况下实际发生的,它可以是很难说。如果调用失败,系统将能够返回-1,并设置错误号来反映错误。如果没有失败那么误差可能在孩子发生和更加难以找到。这可能是你的平台上系统分叉,以确保您有权限执行适当的文件,并设置错误号来反映,但也许不是。

As far as what is actually happening in this case, it can be difficult to tell. If the fork call fails then system will be able to return -1 and set errno to reflect the error. If the fork did not fail then the error may have happened in the child and be more difficult to locate. It may be possible that on your platform system might do some tests before forking to insure that you have permission to execute the appropriate files and set errno to reflect that, but maybe not.

您应该看看 PERROR 功能的情况下打印出的错误消息错误号设置。

You should look into the perror function to print out error messages in the case that errno is set.

如果在故障发生叉和儿童中,那么你要么需要得到shell来告诉你更多关于发生了什么,或者让shell脚本。这可能是通过在脚本回声语句同样在C程序中使用打印语句。

If the failure happens after fork and within the child then you either need to get the shell to tell you more about what is happening, or get the shell script to. This may be by including echo statements in the script similarly to using print statements in your C programs.

您也应该看看访问函数测试,如果你有权限读取和/或执行文件。

You should also look into the access function to test if you have permission to read and/or execute files.

如果您正在使用Linux,那么你应该能够做到:

If you are using Linux then you should be able to do:

strace -o my_program.strace -f ./my_program

ltrace -o my_program.ltrace -f -S ./my_program

然后检查跟踪文件(在 -o )来看看什么程序和内核互相说。 ltrace 着眼于如何在程序会谈库函数,而strace的着眼于系统调用,但 -S 告诉ltrace也看系统调用。在 -f 参数告诉他们既要跟踪程序的儿童,因为他们创建的。

and then examine the trace files (after the -o) to look at what the programs and kernel say to each other. ltrace looks at how the program talks to library function, while strace looks at system calls, but the -S tells ltrace to also look at system calls. The -f argument tells them both to trace children of the program as they are created.

正如我所提到系统 POSIX的制度下,应该使用 / bin / sh的或兼容的外壳。这并不意味着 / bin / sh的将不会运行 /斌/ KSH 运行脚本(或内核不会使用#!行的脚本文件来做到这一点)的开始,但它可能是一个问题。有一些方法可以运行外壳脚本,使得该行不是用来知道哪个壳被使用。最值得注意的是:

As I mentioned system under a Posix system should use /bin/sh or a compatible shell. This doesn't mean that /bin/sh won't run /bin/ksh to run your script (or that the kernel won't use the #! line at the beginning of the script file to do this), but it could be a problem. There are ways to run shell scripts so that this line is not used to know which shell is to be used. The most notable is:

. myshell.sh

的周期和空间基本上是试图将文件的文本转储到当前shell会话,而不是在另一个进程中运行(这是建立一个环境非常有用)。如果你是做:

The period and space essentially tries to dump the text of the file into the current shell session rather than run it in another process (this is useful for setting up an environment). If you were to be doing:

int x = system(". myshell.sh");

然后,可能是一个问题。

Then that could be a problem.

这篇关于C程序调用shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆