简体   繁体   English

Mac Bash 脚本:每 7 的倍数打开一个终端窗口并输入命令

[英]Mac Bash Script: For every multiple of 7 open a terminal window & type command

Essentially all I have is a loop from 0-1000, but I want to make it so that every time a number is a multiple of 7 occurs, open a terminal window and type the following lines:基本上我所拥有的只是一个从 0 到 1000 的循环,但我想让它每次出现 7 的倍数时,打开一个终端窗口并输入以下几行:

  1. cd desktop/testfolder cd 桌面/测试文件夹
  2. node testScript.js节点 testScript.js
  3. 7 <---- the multiple of 7 goes here 7 <---- 7 的倍数在这里

So for example if the loop gets to 49 the commands would be:因此,例如,如果循环达到 49,则命令将是:

  1. cd desktop/testfolder cd 桌面/测试文件夹
  2. node testScript.js节点 testScript.js
  3. 49 49

This is what I have so far:这是我到目前为止:

#!/bin/bash

shopt -s nocasematch
read -p " Execute script? (y/n): " response
if [[ $response == y ]]; then
    printf " Loading....\\n"
    for ((x = 0; x<1000; x++)); do

      #add a check if x is a multiple of 7 here...

      printf " Open %s Terminal\\n" $x
      osascript -e 'tell application "Terminal" to do script "cd desktop//testfolder\
node myScript.js\
MULTIPLE OF 7 HERE"' >/dev/null

    done
fi
shopt -u nocasematch

I have got the terminal opening & first two commands sorted but I can't figure out how to add a check for x to make sure its a multiple of 7, and I can't work out how to get the x integer into the terminal "do script" string.我已经打开终端并排序了前两个命令,但我不知道如何添加x的检查以确保它是 7 的倍数,而且我无法弄清楚如何将 x 整数输入终端“执行脚本”字符串。

Any help is appreciated!任何帮助表示赞赏!

Instead of代替

   for ((x = 0; x<1000; x++)); do

      #add a check if x is a multiple of 7 here...

you can do:你可以做:

for ((x = 0; x<1000; x=x+7)); do

Also, in order to provide this string, if I get correctly what you try to do另外,为了提供此字符串,如果我正确理解您尝试执行的操作

tell application "Terminal" to do script "cd desktop//testfolder\
node myScript.js\
7"

you can do this, in the bash shell:您可以在 bash shell 中执行此操作:

str="tell application \"Terminal\" to do script \"cd desktop//testfolder\\
node myScript.js\\
$x\""
osascript -e "$str"

Got it to work using @thanasisp's for ((x = 0; x<1000; x=x+7)); do使用@thanasisp for ((x = 0; x<1000; x=x+7)); do for ((x = 0; x<1000; x=x+7)); do

And then to get the $x in the do script string I did the following:然后为了在 do 脚本字符串中获取 $x,我执行了以下操作:

osascript -e 'tell application "Terminal" to do script "cd desktop//testfolder\
node myScript.js\
'$x'"'

Just got a bit confusing with all the single and double quotes I guess...我猜对所有的单引号和双引号有点困惑......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM