Automator Variable in shell script - macos

Is it possible to pass an Automator variable into a shell script?

To complement #Ned Deily's answer:
(Written as of OS X 10.9.2, still current as of OSX 10.10)
It is often not necessary to create and use explicit variables in Automator (using the Set Value of Variable and Get Value of Variable actions).
The previous action's output is automatically passed to a Run Shell Script action.
By default, the data is passed via stdin, however.
If you want it passed as arguments ($1, $2, ... - also accessible as an array via $#) instead, select as arguments from the Pass input: list on the right, as illustrated here:
In this example, the selected Finder items are passed as POSIX-style paths to the shell script.
That said, having a shell script process the data via stdin (using read -r in a loop) works, too:

Drag & drop
Store path in filename variable
Prompt window for input value in next variable
get filename variable
Store prompt value in rename_to
$1 – rename_to value, $2 – filename value

Related

Shell command SET -X mean in script [duplicate]

This question already has answers here:
What does `set -x` do?
(3 answers)
Closed 4 years ago.
So I have a file deploy.sh, and it has the shell script. Since I know about it, I have a little confusion, that is what does that set -x actually means.
After running the file I have observed that the command written after it in the file gets mentioned in the terminal with a + sign.
Like if I have this,
#!/bin/bash
set -x
ng build
So the output mentions +ng build, and when I comment the set -x from the file, everything executes, but the later commands does not show up in the terminal.
I have researched about it, but specifically couldn't find the real meaning and work of this particular command.
You can read the bash online manual for set:
-x
Print a trace of simple commands, for commands, case commands, select
commands, and arithmetic for commands and their arguments or
associated word lists after they are expanded and before they are
executed. The value of the PS4 variable is expanded and the resultant
value is printed before the command and its expanded arguments.
So it does exactly what you described.
It's a bit hard to find but this is from the bash man page:
set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
Without options, the name and value of each shell variable are
displayed in a format that can be reused as input for setting or
resetting the currently-set variables. Read-only variables can‐
not be reset. In posix mode, only shell variables are listed.
The output is sorted according to the current locale. When
options are specified, they set or unset shell attributes. Any
arguments remaining after option processing are treated as val‐
ues for the positional parameters and are assigned, in order, to
$1, $2, ... $n. Options, if specified, have the following
meanings:
[...]
-x After expanding each simple command, for command, case
command, select command, or arithmetic for command, dis‐
play the expanded value of PS4, followed by the command
and its expanded arguments or associated word list.
So basically it's a debug option. It does not only execute all the commands but also prints them before it executes them (to stderr).

Using a parameter with exec in Ruby

I'm trying to execute the command exec when I give it a parameter by console, but I don´t know how to make it.
exec('ls -l #{argv[1]}')
Argv[1] is the parameter I pass by console but it doesn´t do anything.
Unless you need your command to be executed by a shell (such as, you redirect to/from a file), you can pass a list of arguments to exec:
exec 'ls', '-l', ARGV[1]
You're aware that exec replaces the running ruby process? Do you want system instead?
https://ruby-doc.org/core-2.5.0/Process.html#method-c-exec
https://ruby-doc.org/core-2.5.0/Kernel.html#method-i-system
There are several small issues in your code:
variables are not interpolated in single-quote strings; this means your script always tries to execute ls -l #{argv[1]} (as it is written here).
there is no variable, constant or method of class Object that is named argv; there is a global constant named ARGV that contains the command line arguments.
ARGV does not contain the script name (it is stored in a separate property) but only its positional parameters; consequently, the first argument in the command line is stored at index 0, not 1.
Putting together all of the above, your script should be:
exec("ls -l #{ARGV[0]}")

Variables in Automator is not working?

I'm trying to set variables in Automator but apparently it's not working?
It only outputs the latest variable; "Var2". What am I doing wrong?
This is what I'm trying to do
Ask for text
Set value of variable: Var1
Ask for text
Set value of variable Var2
Get value of variable Var1
Get value of variable Var2
Run Shell script (as arguments):
for f in "$#"
do
echo "$1" "$2"
done
The following Automator workflow works for me with the following actions, and make note of the Options settings:
Ask for Text
Set Value of Variable
Variable: [Var1]
Ask for Text
Options
[√] Ignore this action's input
Set Value of Variable
Variable: [Var2]
Get Value of Variable
Variable: [Var1]
Options
[√] Ignore this action's input
Get Value of Variable
Variable: [Var2]
Run Shell Script
Shell: [/bin/bash] Pass input: [as arguments]
echo "$1" "$2"
In the Results pane of the Run Shell Script action it shows what was typed in both Ask for Text actions, which were of course set to the Set Value of Variable actions.
For example, I typed "This is Var1" and "This is Var2" respectively, and the Results pane of the Run Shell Script action showed:
This is Var1 This is Var2
Note in the image below that I did not check the [] Require an answer check box in the Ask for Text action's Options, however this was just for testing and I did add text in each instance. For the real thing, you may want to check that check box.
Also note that by checking the [√] Ignore this action's input check box in the given actions, there is a break between that action and the previous action as noted by no visible connection compared to the ones that are visibly connected.

Changing the value of a export variable from a bash script

I did the following from a bash shell.
export myPath="/home/user/dir"
Then I verified the value of this by 'echo'ing this value from both shell and a inside a bash script. Both worked fine.
Then I tried setting the value of this variable from inside a script like this.
myPath="/home/user/newdir"
and tried printing this variable from shell. I thought the variable will hold the updated value, but it was showing the old value.
How can I update the value from a script? I am looking to do it without using source if possible.
To make the variables persist after the script finishes, you have to run it using the source command:
When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

Difference between launching a script with ./script.sh and . ./script.sh

Please tell me what is the difference in bash shell between launching a script with
./script.sh and . ./script.sh?
As klausbyskov says, the first form requries that the file have its executable permission bit set.
But more importantly, the first form executes the script in a separate process (distinct from, independent of, and unable to make changes in the shell that launched it). The second form causes the initial shell to directly run the commands from the file (as if you had typed them into the shell, or as if they were included in the script that does the ‘sourcing’).
A script that contains FOO=bar; export FOO will have not create an exported FOO environment variable in the shell that runs the first variant, but it will create such a variable in a shell that runs the second variant.
The second form (‘sourcing’) is a bit like a #include in C.
The first requires the file to have the +x flag set. The second uses the . command aka "source", described here.

Resources