Oddacon
Oddacon

Reputation: 319

how to exec multiple commands from a single variable from a tcl file

I am trying to make a custom MOTD header using a tcl file. I've already been succuessful in adding the commands to the last line of the /etc/profile

cowsay -f $(ls /usr/share/cowsay/cows/  | shuf -n1) $(whoami), $(fortune)

I want to add this into my existing MOTD but I do not know the proper syntax to exec multiple commands without the pipe break command. As you can see below I have tried:

#!/usr/bin/env tclsh

# * Variable
set cows [exec -- /usr/bin/whoami | /usr/games/fortune | cowsay]


# * Output

puts "$cows"

which outputs the fortune and cowsay fine but, I cannot seem to get the whoami command to exec up with the other commands.

\   ^__^
 \  (oo)\_______
    (__)\       )\/\
        ||----w |
        ||     ||

Any help regarding how multiple commands are executed from within the syntax of the tcl format would greatly be appreciated, thanks y'all.

Upvotes: 1

Views: 5337

Answers (2)

slebetman
slebetman

Reputation: 113878

The answer by iagreen is of course correct and probably more maintainable, but to answer your subquestion: how to translate $(some command) into tcl:

In bash, the $(...) syntax executes the string captured by the parenthesis by evaluating it in another shell - a new instance of bash often referred to as a subshell.

In tcl, the exec command executes its arguments as a list of words to be evaluated in a subshell.

So, putting two and two together the correct translation of $(...) is [exec ...].

Therefore, the direct translation of this:

cowsay -f $(ls /usr/share/cowsay/cows/  | shuf -n1) $(whoami), $(fortune)

is this:

exec cowsay -f [exec ls /usr/share/cowsay/cows/ | shuf -n1] \
               [exec whoami], [exec fortune]

Which is basically the same as the answer given by iagreen.

Upvotes: 4

iagreen
iagreen

Reputation: 31996

The problem with your approach is fortune ignores stdin.

It looks like you are building a string to pass to cowsay. Why not build the string in pieces using Tcl strings. For example,

#!/usr/bin/env tclsh

# * Variable
set cowsParam [exec /usr/bin/whoami]
append cowsParam ", " [exec /usr/games/fortune]
set cowImage [exec ls /usr/share/cowsay/cows/  | shuf -n1]
set cows [exec cowsay -f $cowImage $cowsParam]


# * Output

puts "$cows"

Upvotes: 3

Related Questions