简体   繁体   English

OSX Bash Shell脚本无法运行

[英]OSX Bash Shell Script Does Not Run

I'm having a pretty basic shell script that's supposed to run at user login. 我有一个非常基本的shell脚本,应该在用户登录时运行。 To achieve this, I followed the guide for Automator from the first answer in this topic: Running script upon login mac 为此,我按照本主题的第一个答案遵循了Automator指南: 登录mac时运行脚本

But somehow nothing happens. 但是不知何故。 I also tried to create an application using the script editor with do shell script /blabla/git_credentials.sh which responded with a permission denied. 我还尝试使用脚本编辑器创建一个带有do shell script /blabla/git_credentials.sh的应用程序,该应用程序以拒绝权限进行响应。

I don't see whats wrong here. 我看不出这里有什么问题。

Oh, here's the script: 哦,这是脚本:

echo ""
echo "Setup Git Credentials"
echo "*********************"
echo "Please enter your first name: "
read fname
echo "Please enter your last name: "
read lname
echo "Please enter your mail address: "
read email
git config --global --remove-section user
git config --global user.name "$fname $lname"
git config --global user.email $email
echo "Credentials set." 

Edit: I just found out that the script is being run at login, but it neither opens up a terminal nor waits for my user inputs, I have just an empty Git config after every startup. 编辑:我刚刚发现该脚本正在登录时运行,但是它既不打开终端也不等待我的用户输入,每次启动后我只有一个空的Git配置。 I 'achieved' this using the script editor with do shell script "$HOME/git_credentials.sh" and saving it as an application, then putting it into the login items. 我使用脚本编辑器和do shell script "$HOME/git_credentials.sh"将其do shell script "$HOME/git_credentials.sh"并将其保存为应用程序,然后将其放入登录项中。

The problem is that your Automator's shell script isn't connected to STDIN (ie your keyboard). 问题是您的Automator的Shell脚本未连接到STDIN(即您的键盘)。 It may run the shell script, but there's no way to pass it input since there's no terminal. 它可以运行shell脚本,但是由于没有终端,因此无法传递输入。

What you need to do is run the Automator action: Ask for Text to get your input. 您需要做的是运行“自动化”操作:“ 询问文本”以获取输入。

What I found I had to do was Ask for Text , and then Set the Value of the Variable . 我发现我要做的是询问文本 ,然后设置变量的值 I do this for each variable I want as input. 我为每个要输入的变量执行此操作。

Once I get all of the variables I want, I then run Get the Value of the Variable for each of the variables. 一旦获得所需的所有变量,就可以为每个变量运行“ 获取变量的值”。 This puts the variables into $* for the shell script to pull up. 这会将变量放入$*以便shell脚本加载。

Now, you can execute the Automator action Run Shell Script with Pass Input as arguments . 现在,您可以使用Pass Input 作为参数执行Automator操作Run Shell Script You can refer to them as $1 , $2 , etc. 您可以将它们称为$1$2等。

I suggest to try this with a simple script and see if it then works. 我建议尝试使用一个简单的脚本来尝试一下,然后看看它是否有效。 The problem is that the whole thing may execute in a sub-shell, so once the automator action ends, you lose the values of the variables you've set. 问题在于整个过程可能都在子shell中执行,因此一旦automator操作结束,您将丢失已设置的变量的值。 I simply don't have enough experience with Automator to know exactly how it works. 我只是对Automator缺乏足够的经验,无法确切了解其工作原理。

I suspect you script is not currently executable. 我怀疑您的脚本当前不可执行。

Try fixing this by running: chmod +x /blabla/git_credentials.sh 尝试通过运行以下chmod +x /blabla/git_credentials.sh修复此问题: chmod +x /blabla/git_credentials.sh

or 要么

chmod 755 /blabla/git_credentials.sh

Or you are missing #!/bin/bash at the top of your script? 还是您的脚本顶部缺少#!/bin/bash Or is this just a part of it? 还是这只是一部分?

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

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