首页 > 解决方案 > 编译后运行 shell 脚本时出现问题

问题描述

我创建了一个 shell 脚本,当从命令行调用它并给它每个参数时,它运行完美。但是,当我使用我的 make 文件编译脚本时,它会忽略没有给出参数的情况并且什么也不打印。

如果没有通过命令行传入参数,我的逻辑是否有问题?

#!/bin/bash
# findName.sh
searchFile="/acct/common/CSCE215-Fall19"

if [[ $1 = "" ]] ; then
echo "ERROR ARGUMENT NEEDED"
exit 2
fi

grep -i $1 ${searchFile}
if [[ $? = "1" ]] ; then
  echo "$1 was not found in ${searchFile}"
fi

编辑

#makefile for building

findName:       main.o
    g++ -g main.o -o findName

# main
main.o:         main.cpp
        g++ -c -g main.cpp

clean:
        /bin/rm -f findName *.o

backup:
        tar cvf proj.tar * cpp Makefile *.sh readme

主文件

#include <string>
#include <cstdlib>

int main(int argc, char* argv[])
{
    std::string command = "./findName.sh";

    if(argc == 2)
        std::system((command + " " + argv[1]).c_str());
}

标签: linux

解决方案


推荐阅读