当我从c ++程序

时间:2016-08-23 11:54:38

标签: c++ linux bash shell

我正在尝试编写一个程序,以便更轻松地管理Arduino项目。所以我编写了bash脚本,为我创建了所有必需的文件夹和文件,当我执行它时,我像champ一样运行。因为我想在脚本中的工作终端中更改目录,所以我就像这样运行脚本

. ./initialize.sh

这也很有效,但因为我正在编写C ++程序,从程序中获取这个脚本让我很头疼。

所以在程序中我运行这样的脚本:

system(". /usr/lib/avrduino/script/initialize.sh");

然后当我运行程序时出现此错误:

  

sh:25:/usr/lib/avrduino/script/initialize.sh:语法错误:“(”unexpected(expecting“}”)

从程序中运行脚本,如下所示:

system("/usr/lib/avrduino/script/initialize.sh");

无误地工作,但它在子shell中运行。

语法错误指向脚本

中的此行
options=("uno" "mega" "mega2560" "atmega8" "atmega168" "atmega328" "pro" "pro5v" "pro328" "pro5v328")

为什么当我在程序之外运行这个脚本时,它像champ一样工作,但是从程序运行这个脚本你有问题吗?

编辑: 脚本代码

   #!/bin/bash

BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BROWN='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LGRAY='\033[0;37m'
DGRAY='\033[1;30m'
LRED='\033[1;31m'
LGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LBLUE='\033[1;34m'
LPURPLE='\033[1;35m'
LCYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m'

makefile()
{
  BOARD="default"
  PS3='Chose your board: '
  options=("uno" "mega" "mega2560" "atmega8" "atmega168" "atmega328" "pro" "pro5v" "pro328" "pro5v328")
  select opt in "${options[@]}"
  do case $opt in 
    "uno") BOARD="uno"
    cp -r /usr/lib/avrduino/data/boards_info/uno/board-info.h .
    MCU="atmega328P"
    F_CPU="16000000UL"
    ;;
  "mega") BOARD="mega" 
    cp -r /usr/lib/avrduino/data/boards_info/mega/board-info.h .
    F_CPU="16000000UL"
    ;;
  "mega2560") BOARD="mega2560"
    cp -r /usr/lib/avrduino/data/boards_info/mega2560/board-info.h .
    F_CPU="16000000UL"
    ;;
  "atmega8") BOARD="atmega8"
    cp -r /usr/lib/avrduino/data/boards_info/atmega8/board_-nfo.h .
    MCU="atmega8"
    F_CPU="16000000UL"
    ;;
  "atmega168") BOARD="atmega168"
    cp -r /usr/lib/avrduino/data/boards_info/atmega168/board-info.h .
    MCU="atmega168"
    F_CPU="16000000UL"
    ;;
  "atmega328") BOARD="atmega328"
    cp -r /usr/lib/avrduino/data/boards_info/atmega328/board-info.h .
    MCU="atmega328P"
    F_CPU="16000000UL"
    ;;
  "pro") BOARD="pro"
    cp -r /usr/lib/avrduino/data/boards_info/pro/board-info.h .
    MCU="unknow"
    F_CPU="16000000UL"
    ;;
  "pro5v") BOARD="pro5v"
    cp -r /usr/lib/avrduino/data/boards_info/pro5v/board-info.h .
    MCU="unknown"
    F_CPU="16000000UL"
    ;;

  "pro328") BOARD="pro328"
    cp -r /usr/lib/avrduino/data/boards_info/pro328/board-info.h .
    MCU="atmega328P"
    F_CPU="16000000UL"
    ;;
  "pro5v328") BOARD= "pro5v328"
    cp -r /usr/lib/avrduino/data/boards_info/pro5v328/board-info.h .
    MCU="atmega328P"
    F_CPU="16000000UL"
    ;;
  *)
    echo "Error : Input is not valid"
    echo "Exiting..."
    return 1 
    ;;
    esac
    break
  done
  [ -e Makefile ] && rm Makefile
  read -p "Do you want to configure your Makefile settings [Y/n]: " CONFIGURE
  if { [ "$CONFIGURE" == "Y" ] || [ "$CONFIGURE" == "y" ]; }; then
    read -p "Enter your MCU: " MCU
    read -p "Enter F_CPU: " F_CPU
  fi
  read -p "Enter ARDUINO_PORT: " ARDUINO_PORT
  echo "ARDUINO_DIR = /usr/share/arduino">>Makefile
  echo "BOARD_TAG = $BOARD">>Makefile
  echo "ARDUINO_PORT = $ARDUINO_PORT">>Makefile
  echo "NO_CORE = 1">>Makefile
  echo "AVRDUDE_ARD_PROGRAMMER = arduino">>Makefile
  echo "HEX_MAXIMUM_SIZE = 30720">>Makefile
  echo "AVRDUDE_ARD_BAUDRATE = 115200">>Makefile
  echo "#ISP_LOW_FUSE = 0xFF">>Makefile
  echo "#ISP_HIGH_FUSE = 0xDA">>Makefile
  echo "#ISP_EXT_FUSE = 0x05">>Makefile
  echo "#ISP_LOCK_FUSE_PRE = 0x3F">>Makefile
  echo "#ISP_LOCK_FUSE_POST = 0x0F">>Makefile
  echo "MCU = $MCU">>Makefile
  echo "F_CPU = $F_CPU">>Makefile
  echo "VARIANT = standard">>Makefile
  echo "ARDUINO_LIBS =">>Makefile
  echo "include /usr/share/arduino/Arduino.mk">>Makefile
  echo "$BOARD|$MCU|" >> .avrduino.txt

  clear
  echo -e "${LGREEN}Makefile settings${NC}"
  echo -e "${LBLUE}ARDUINO_DIR = ${LRED}/usr/share/arduino ${NC}"
  echo -e "${LBLUE}BOARD_TAG = ${LRED}$BOARD${NC}"
  echo -e "${LBLUE}ARDUINO_PORT = ${LRED}$ARDUINO_PORT${NC}"
  echo -e "${LBLUE}NO_CORE = ${LRED}1${NC}"
  echo -e "${LBLUE}AVRDUDE_ARD_PROGRAMMER = ${LRED}arduino${NC}"
  echo -e "${LBLUE}HEX_MAXIMUM_SIZE = ${LRED}30720${NC}"
  echo -e "${LBLUE}AVRDUDE_ARD_BAUDRATE = ${LRED}115200${NC}"
  echo -e "${DGRAY}#ISP_LOW_FUSE = ${RED}0xFF${NC}"
  echo -e "${DGRAY}#ISP_HIGH_FUSE = ${RED}0xDA${NC}"
  echo -e "${DGRAY}#ISP_EXT_FUSE = ${RED}0x05${NC}"
  echo -e "${DGRAY}#ISP_LOCK_FUSE_PRE = ${RED}0x3F${NC}"
  echo -e "${DGRAY}#ISP_LOCK_FUSE_POST = ${RED}0x0F${NC}"
  echo -e "${LBLUE}MCU = ${LRED}$MCU${NC}"
  echo -e "${LBLUE}F_CPU = ${LRED}$F_CPU${NC}"
  echo -e "${LBLUE}VARIANT = ${LRED}standard${NC}"
  echo -e "${LBLUE}ARDUINO_LIBS =${NC}"

}

initializeProject()
{
  read -p "Project name: " PROJECT_NAME
  if [ ! -e PROJECT_NAME ]; then
    mkdir $PROJECT_NAME
    cd $PROJECT_NAME
    makefile #Call function that makes makefile
    cp -r /usr/lib/avrduino/data/include/ .
    echo -e "${LGREEN}Project created successfully ${NC}"
  else 
    echo "AVRduino: Project with name [ $PROJECT_NAME ] already exists. "
    echo "AVRduino: Stop project wizard and exit."
  fi
}

clear
initializeProject

2 个答案:

答案 0 :(得分:2)

#!/bin/bash不会将脚本作为进程执行,只会将其加载到当前的shell进程中。

在这种情况下,您的“shebang”行#! doodle poodle noodle只是一个评论 (您可以将system放在那里,它也可以运行。)

当您使用/bin/sh时,它会在bash中执行,因此您的chmod +x /usr/lib/avrduino/script/initialize.sh 脚本会出现语法错误。

执行脚本的一种方法是使它们可执行:

system

然后您可以直接将其传递给system("/usr/lib/avrduino/script/initialize.sh");

bash

或者,您可以在system("/bin/bash /usr/lib/avrduino/script/initialize.sh"); 中明确地执行它:

initialize.sh

好的,这是一种可以解决“更改目录”问题的方法:

重写.bashrc所以它将项目名称作为参数,而不是以交互方式请求它(这是正常的Unix工具的工作原理,所以坚持下去)。

然后将以下内容添加到make_project() { /usr/lib/avrduino/script/initialize.sh "$1" && cd "$1" }

make_project foo

然后你可以说ELK并转移到目录“foo”。

答案 1 :(得分:1)

最有可能是错误的shebang导致默认shell运行 - 确保shebang位于第一行的开头

#!/bin/bash
# rest of script

如果不起作用,请将系统调用更改为

system("/bin/bash /usr/lib/avrduino/script/initialize.sh");