C语言调用shell脚本:
通过想通过C程序调用shell脚本使用sytem()即可如:
system(“nohup /home/yuxc/ttt.sh”)
要想通过C语言命令,执行ttt.sh,通过c编译后的c程序必须放在ttt.sh同一路径下,
并且赋予该编译后的C程序(XXX.out)对应的权限(为方便直接给7777权限chmod 7777 XXX.out)
不然会出现找不到该文件路径的错误,或者无法调用某些函数如(setuid())
C语言修改当前账户权限等级为root级代码:
#include
#include
#include
#include
#define LEN 256
void printHelp(char *p)
{
printf("Usage: %{start|stop}\n", p);
}
int main(int num, char *args[])
{
char str[LEN]={0};
int i=0;
if(num<2)
{
printHelp(args[0]);
return 1;
}
system("whoami");
if(setuid(0)==-1)
{
printf(args[0]);
printf(args[1]);
printf("setuid errot\n");
return 2;
}
if(strcmp(args[1],"start")==0)
{
sprintf(str,"/home/yuxc/javaclass/ttt
%s", args[1]);
system("nohup /home/yuxc/javaclass/ttt.sh");
i=system(str);
system("whoami");
return i;
}
system("whoami");
return 0;
}
SHELL脚本调用java类:
在shell中调用java类直接在shell脚本中写java
XXX即可(XXX为class名),并且该shell脚本也要放在和
java同一路径下,并且shell要有对应的权限,不然也会报找不到该文件路径的错误
shell调用java类脚本
java XXXX
linux中的命令:
gcc编译C语言命令:gcc 被编译文件名
mv移动文件到指定地址:mv 被移动文件名
将被移动到的地址
su切换用户:su 切换到的用户名
nohup执行shell脚本命令:nohup shell脚本名
chmod赋予指定文件权限:chmod 777 被赋权文件名
echo打印字符串:echo 222222
setuid(0)表示赋与当前用户root账户权限返回值0表示赋权成功,-1表示赋权失败