如何让我现在可执行的shell脚本接受一个字符串

时间:2015-02-15 22:20:53

标签: c++ shell

我有一个shell脚本findName.sh,它将找到一个与我输入的用户ID匹配的名称。

#!/bin/sh 
# for use , in some  project.....

if [ $# -eq 1 ]; then
# if there is exactly one command line arg used with the command
# do something(s).
    if [ -s /class/rolls/CSCE215-803 ]; then
    grep $1 /class/rolls/CSCE215-803 > ./output

if [ -s ./output ]; then
    cut -c 1-30 ./output
else
    echo "Sorry that person is not in CSCE215-803"
fi
else
    echo "CSCE215-803 file does not exist!"
    fi
else

 echo "Incorrect number of arguments"
 exit 1
fi

这很好......

但是当我把它放入c ++文件main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main () {

    system("./findName.sh");

    return 0;
}

当它被编译为findName并且我尝试执行它,即(./findName someUserId)时,它将始终返回“参数数量不正确”。我知道这可能是一个简单的修复,但我是新手。

1 个答案:

答案 0 :(得分:0)

惊讶?你的shell脚本和你的C程序是两个完全不同的东西。使用以下行

system("./findName.sh");

你只调用你的脚本,但你没有传递任何参数。你应该像这样调用你的脚本

system("./findName.sh hereisyourargument");