1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:17:44 +00:00

Shell: Fix job control and backgrounding

This patchset makes the shell capable of lazily resolving and executing
sequences of commands, to allow for putting logical sequences in the
background.
In particular, it enables And/Or/Sequence nodes to be run in the background,
and consequently unmarks them as `would_execute`.
Doing so also fixes job control to an extent, as jobs are now capable of
having 'tails', so sequences can be put in the background while
preserving their following sequences.
This commit is contained in:
AnotherTest 2020-09-01 08:42:16 +04:30 committed by Andreas Kling
parent a2a54f459f
commit 715e11f692
10 changed files with 180 additions and 151 deletions

View file

@ -184,22 +184,9 @@ void And::dump(int level) const
RefPtr<Value> And::run(RefPtr<Shell> shell) RefPtr<Value> And::run(RefPtr<Shell> shell)
{ {
auto left = m_left->run(shell); auto commands = m_left->run(shell)->resolve_as_commands(shell);
ASSERT(left->is_job()); commands.last().next_chain.append(NodeWithAction { *m_right, NodeWithAction::And });
return create<CommandSequenceValue>(move(commands));
auto* job_value = static_cast<JobValue*>(left.ptr());
const auto job = job_value->job();
if (!job) {
// Something has gone wrong, let's just pretend that the job failed.
return job_value;
}
shell->block_on_job(job);
if (job->exit_code() == 0)
return m_right->run(shell);
return job_value;
} }
void And::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata) void And::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
@ -354,16 +341,11 @@ RefPtr<Value> Background::run(RefPtr<Shell> shell)
// all but their last subnode before yielding to this, causing a command // all but their last subnode before yielding to this, causing a command
// like `foo && bar&` to effectively be `foo && (bar&)`. // like `foo && bar&` to effectively be `foo && (bar&)`.
auto value = m_command->run(shell)->resolve_without_cast(shell); auto value = m_command->run(shell)->resolve_without_cast(shell);
if (value->is_job()) { ASSERT(!value->is_job());
auto job = static_cast<JobValue*>(value.ptr())->job();
job->set_running_in_background(true);
job->set_should_announce_exit(true);
return value;
}
auto commands = value->resolve_as_commands(shell); auto commands = value->resolve_as_commands(shell);
auto& last = commands.last(); for (auto& command : commands)
last.should_wait = false; command.should_wait = false;
return create<CommandSequenceValue>(move(commands)); return create<CommandSequenceValue>(move(commands));
} }
@ -925,7 +907,7 @@ void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(Ref
} }
auto& last_in_commands = commands.last(); auto& last_in_commands = commands.last();
last_in_commands.redirections.append(FdRedirection::create(STDOUT_FILENO, pipefd[1], Rewiring::Close::Destination)); last_in_commands.redirections.prepend(FdRedirection::create(STDOUT_FILENO, pipefd[1], Rewiring::Close::Destination));
last_in_commands.should_wait = false; last_in_commands.should_wait = false;
last_in_commands.should_notify_if_in_background = false; last_in_commands.should_notify_if_in_background = false;
last_in_commands.is_pipe_source = false; last_in_commands.is_pipe_source = false;
@ -1042,16 +1024,10 @@ void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(Ref
return; return;
} }
RefPtr<Job> last_job; auto jobs = shell->run_commands(commands);
for (auto& job : shell->run_commands(commands)) { if (!jobs.is_empty())
shell->block_on_job(job); callback(create<JobValue>(&jobs.last()));
last_job = move(job);
}
callback(create<JobValue>(move(last_job)));
return;
} }
RefPtr<Value> Execute::run(RefPtr<Shell> shell) RefPtr<Value> Execute::run(RefPtr<Shell> shell)
@ -1150,7 +1126,7 @@ RefPtr<Value> IfCond::run(RefPtr<Shell> shell)
if (cond_job->signaled()) if (cond_job->signaled())
return create<ListValue>({}); // Exit early. return create<ListValue>({}); // Exit early.
if (cond_job->exit_code() == 0) { if (shell->last_return_code == 0) {
if (m_true_branch) if (m_true_branch)
return m_true_branch->run(shell); return m_true_branch->run(shell);
} else { } else {
@ -1287,23 +1263,9 @@ void Or::dump(int level) const
RefPtr<Value> Or::run(RefPtr<Shell> shell) RefPtr<Value> Or::run(RefPtr<Shell> shell)
{ {
auto commands = m_left->run(shell)->resolve_as_commands(shell);
auto left = m_left->run(shell); commands.last().next_chain.empend(*m_right, NodeWithAction::Or);
ASSERT(left->is_job()); return create<CommandSequenceValue>(move(commands));
auto* job_value = static_cast<JobValue*>(left.ptr());
const auto job = job_value->job();
if (!job) {
// Something has gone wrong, let's just pretend that the job failed.
return m_right->run(shell);
}
shell->block_on_job(job);
if (job->exit_code() == 0)
return job_value;
return m_right->run(shell);
} }
void Or::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata) void Or::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)
@ -1549,6 +1511,7 @@ RefPtr<Value> Sequence::run(RefPtr<Shell> shell)
execute_node = create<AST::Execute>(m_right->position(), m_right); execute_node = create<AST::Execute>(m_right->position(), m_right);
return execute_node->run(shell); return execute_node->run(shell);
} }
auto left = m_left->run(shell)->resolve_as_commands(shell); auto left = m_left->run(shell)->resolve_as_commands(shell);
// This could happen if a comment is next to a command. // This could happen if a comment is next to a command.
if (left.size() == 1) { if (left.size() == 1) {
@ -1557,13 +1520,12 @@ RefPtr<Value> Sequence::run(RefPtr<Shell> shell)
return m_right->run(shell); return m_right->run(shell);
} }
auto right = m_right->run(shell)->resolve_as_commands(shell); if (left.last().should_wait)
left.last().next_chain.append(NodeWithAction { *m_right, NodeWithAction::Sequence });
else
left.append(m_right->run(shell)->resolve_as_commands(shell));
Vector<Command> commands; return create<CommandSequenceValue>(move(left));
commands.append(left);
commands.append(right);
return create<CommandSequenceValue>(move(commands));
} }
void Sequence::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata) void Sequence::highlight_in_editor(Line::Editor& editor, Shell& shell, HighlightMetadata metadata)

View file

@ -28,6 +28,7 @@
#include "Forward.h" #include "Forward.h"
#include "Job.h" #include "Job.h"
#include <AK/InlineLinkedList.h>
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h> #include <AK/RefPtr.h>
@ -174,13 +175,30 @@ public:
pid_t pgid { -1 }; pid_t pgid { -1 };
}; };
struct NodeWithAction {
mutable NonnullRefPtr<Node> node;
enum Action {
And,
Or,
Sequence,
} action;
NodeWithAction(Node& node, Action action)
: node(node)
, action(action)
{
}
};
struct Command { struct Command {
Vector<String> argv; Vector<String> argv;
NonnullRefPtrVector<Redirection> redirections; NonnullRefPtrVector<Redirection> redirections;
mutable RefPtr<Pipeline> pipeline;
bool should_wait { true }; bool should_wait { true };
bool is_pipe_source { false }; bool is_pipe_source { false };
bool should_notify_if_in_background { true }; bool should_notify_if_in_background { true };
mutable RefPtr<Pipeline> pipeline;
Vector<NodeWithAction> next_chain;
}; };
struct HitTestResult { struct HitTestResult {
@ -215,7 +233,7 @@ public:
} }
CommandValue(Vector<String> argv) CommandValue(Vector<String> argv)
: m_command({ move(argv), {}, {}, true, false, true }) : m_command({ move(argv), {}, true, false, true, nullptr, {} })
{ {
} }
@ -424,7 +442,6 @@ private:
virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override; virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override;
virtual HitTestResult hit_test_position(size_t) override; virtual HitTestResult hit_test_position(size_t) override;
virtual String class_name() const override { return "And"; } virtual String class_name() const override { return "And"; }
virtual bool would_execute() const override { return true; }
RefPtr<Node> m_left; RefPtr<Node> m_left;
RefPtr<Node> m_right; RefPtr<Node> m_right;
@ -458,7 +475,6 @@ private:
virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override; virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override;
virtual HitTestResult hit_test_position(size_t) override; virtual HitTestResult hit_test_position(size_t) override;
virtual String class_name() const override { return "Background"; } virtual String class_name() const override { return "Background"; }
virtual bool would_execute() const override { return m_command->would_execute(); }
RefPtr<Node> m_command; RefPtr<Node> m_command;
}; };
@ -725,7 +741,6 @@ private:
virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override; virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override;
virtual HitTestResult hit_test_position(size_t) override; virtual HitTestResult hit_test_position(size_t) override;
virtual String class_name() const override { return "Or"; } virtual String class_name() const override { return "Or"; }
virtual bool would_execute() const override { return true; }
RefPtr<Node> m_left; RefPtr<Node> m_left;
RefPtr<Node> m_right; RefPtr<Node> m_right;

View file

@ -29,10 +29,11 @@
class Shell; class Shell;
namespace AST { namespace AST {
struct Command;
class Node; class Node;
class Value; class Value;
class SyntaxError; class SyntaxError;
class Pipeline; class Pipeline;
class Rewiring; struct Rewiring;
} }

View file

@ -26,6 +26,7 @@
#include "Job.h" #include "Job.h"
#include "AST.h" #include "AST.h"
#include "Shell.h"
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -70,3 +71,42 @@ bool Job::print_status(PrintStatusMode mode)
return true; return true;
} }
Job::Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Command&& command)
: m_pgid(pgid)
, m_pid(pid)
, m_job_id(job_id)
, m_cmd(move(cmd))
{
m_command = make<AST::Command>(move(command));
set_running_in_background(false);
m_command_timer.start();
}
void Job::set_has_exit(int exit_code)
{
if (m_exited)
return;
m_exit_code = exit_code;
m_exited = true;
if (on_exit)
on_exit(*this);
}
void Job::set_signalled(int sig)
{
if (m_exited)
return;
m_exited = true;
m_exit_code = 126;
m_term_sig = sig;
if (on_exit)
on_exit(*this);
}
void Job::unblock() const
{
if (!m_exited && on_exit)
on_exit(*this);
}

View file

@ -41,9 +41,11 @@
# undef JOB_TIME_INFO # undef JOB_TIME_INFO
#endif #endif
struct LocalFrame;
class Job : public RefCounted<Job> { class Job : public RefCounted<Job> {
public: public:
static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String command, u64 job_id, AST::Pipeline* pipeline = nullptr) { return adopt(*new Job(pid, pgid, move(command), job_id, pipeline)); } static NonnullRefPtr<Job> create(pid_t pid, pid_t pgid, String cmd, u64 job_id, AST::Command&& command) { return adopt(*new Job(pid, pgid, move(cmd), job_id, move(command))); }
~Job() ~Job()
{ {
@ -53,13 +55,15 @@ public:
dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms"; dbg() << "Command \"" << m_cmd << "\" finished in " << elapsed << " ms";
} }
#endif #endif
if (m_pipeline)
m_pipeline = nullptr;
} }
Function<void(RefPtr<Job>)> on_exit;
pid_t pgid() const { return m_pgid; } pid_t pgid() const { return m_pgid; }
pid_t pid() const { return m_pid; } pid_t pid() const { return m_pid; }
const String& cmd() const { return m_cmd; } const String& cmd() const { return m_cmd; }
const AST::Command& command() const { return *m_command; }
AST::Command* command_ptr() { return m_command; }
u64 job_id() const { return m_job_id; } u64 job_id() const { return m_job_id; }
bool exited() const { return m_exited; } bool exited() const { return m_exited; }
bool signaled() const { return m_term_sig != -1; } bool signaled() const { return m_term_sig != -1; }
@ -78,34 +82,12 @@ public:
bool is_running_in_background() const { return m_running_in_background; } bool is_running_in_background() const { return m_running_in_background; }
bool should_announce_exit() const { return m_should_announce_exit; } bool should_announce_exit() const { return m_should_announce_exit; }
bool is_suspended() const { return m_is_suspended; } bool is_suspended() const { return m_is_suspended; }
void unblock() const void unblock() const;
{
if (!m_exited && on_exit)
on_exit(*this);
}
Function<void(RefPtr<Job>)> on_exit;
Core::ElapsedTimer& timer() { return m_command_timer; } Core::ElapsedTimer& timer() { return m_command_timer; }
void set_has_exit(int exit_code) void set_has_exit(int exit_code);
{ void set_signalled(int sig);
if (m_exited)
return;
m_exit_code = exit_code;
m_exited = true;
if (on_exit)
on_exit(*this);
}
void set_signalled(int sig)
{
if (m_exited)
return;
m_exited = true;
m_exit_code = 126;
m_term_sig = sig;
if (on_exit)
on_exit(*this);
}
void set_is_suspended(bool value) const { m_is_suspended = value; } void set_is_suspended(bool value) const { m_is_suspended = value; }
@ -127,18 +109,7 @@ public:
bool print_status(PrintStatusMode); bool print_status(PrintStatusMode);
private: private:
Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Pipeline* pipeline) Job(pid_t pid, unsigned pgid, String cmd, u64 job_id, AST::Command&& command);
: m_pgid(pgid)
, m_pid(pid)
, m_job_id(job_id)
, m_cmd(move(cmd))
{
if (pipeline)
m_pipeline = *pipeline;
set_running_in_background(false);
m_command_timer.start();
}
pid_t m_pgid { 0 }; pid_t m_pgid { 0 };
pid_t m_pid { 0 }; pid_t m_pid { 0 };
@ -153,5 +124,5 @@ private:
mutable bool m_active { true }; mutable bool m_active { true };
mutable bool m_is_suspended { false }; mutable bool m_is_suspended { false };
bool m_should_be_disowned { false }; bool m_should_be_disowned { false };
RefPtr<AST::Pipeline> m_pipeline; OwnPtr<AST::Command> m_command;
}; };

View file

@ -283,7 +283,7 @@ RefPtr<AST::Node> Parser::parse_or_logical_sequence()
if (!right_and_sequence) if (!right_and_sequence)
right_and_sequence = create<AST::SyntaxError>("Expected an expression after '||'"); right_and_sequence = create<AST::SyntaxError>("Expected an expression after '||'");
return create<AST::Or>(create<AST::Execute>(move(and_sequence)), create<AST::Execute>(move(right_and_sequence))); return create<AST::Or>(move(and_sequence), move(right_and_sequence));
} }
RefPtr<AST::Node> Parser::parse_and_logical_sequence() RefPtr<AST::Node> Parser::parse_and_logical_sequence()
@ -305,7 +305,7 @@ RefPtr<AST::Node> Parser::parse_and_logical_sequence()
if (!right_and_sequence) if (!right_and_sequence)
right_and_sequence = create<AST::SyntaxError>("Expected an expression after '&&'"); right_and_sequence = create<AST::SyntaxError>("Expected an expression after '&&'");
return create<AST::And>(create<AST::Execute>(move(pipe_sequence)), create<AST::Execute>(move(right_and_sequence))); return create<AST::And>(move(pipe_sequence), move(right_and_sequence));
} }
RefPtr<AST::Node> Parser::parse_pipe_sequence() RefPtr<AST::Node> Parser::parse_pipe_sequence()

View file

@ -421,15 +421,7 @@ int Shell::run_command(const StringView& cmd)
tcgetattr(0, &termios); tcgetattr(0, &termios);
auto result = command->run(*this); command->run(*this);
if (result->is_job()) {
auto job_result = static_cast<AST::JobValue*>(result.ptr());
auto job = job_result->job();
if (!job)
last_return_code = 0;
else if (job->exited())
last_return_code = job->exit_code();
}
return last_return_code; return last_return_code;
} }
@ -500,8 +492,11 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
} }
int retval = 0; int retval = 0;
if (run_builtin(command, rewirings, retval)) if (run_builtin(command, rewirings, retval)) {
for (auto& next_in_chain : command.next_chain)
run_tail(next_in_chain, retval);
return nullptr; return nullptr;
}
Vector<const char*> argv; Vector<const char*> argv;
Vector<String> copy_argv = command.argv; Vector<String> copy_argv = command.argv;
@ -619,15 +614,19 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
StringBuilder cmd; StringBuilder cmd;
cmd.join(" ", command.argv); cmd.join(" ", command.argv);
auto job = Job::create(child, pgid, cmd.build(), find_last_job_id() + 1, command.pipeline); auto job = Job::create(child, pgid, cmd.build(), find_last_job_id() + 1, AST::Command(command));
jobs.set((u64)child, job); jobs.set((u64)child, job);
job->on_exit = [](auto job) { job->on_exit = [this](auto job) {
if (!job->exited()) if (!job->exited())
return; return;
if (job->is_running_in_background() && job->should_announce_exit()) if (job->is_running_in_background() && job->should_announce_exit())
fprintf(stderr, "Shell: Job %" PRIu64 "(%s) exited\n", job->job_id(), job->cmd().characters()); fprintf(stderr, "Shell: Job %" PRIu64 "(%s) exited\n", job->job_id(), job->cmd().characters());
last_return_code = job->exit_code();
job->disown(); job->disown();
run_tail(job);
}; };
fds.collect(); fds.collect();
@ -635,9 +634,42 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
return *job; return *job;
} }
void Shell::run_tail(const AST::NodeWithAction& next_in_chain, int head_exit_code)
{
switch (next_in_chain.action) {
case AST::NodeWithAction::And:
if (head_exit_code == 0) {
auto commands = next_in_chain.node->run(*this)->resolve_as_commands(*this);
run_commands(commands);
}
break;
case AST::NodeWithAction::Or:
if (head_exit_code != 0) {
auto commands = next_in_chain.node->run(*this)->resolve_as_commands(*this);
run_commands(commands);
}
break;
case AST::NodeWithAction::Sequence:
auto commands = next_in_chain.node->run(*this)->resolve_as_commands(*this);
run_commands(commands);
break;
}
}
void Shell::run_tail(RefPtr<Job> job)
{
if (auto cmd = job->command_ptr()) {
deferred_invoke([=, this](auto&) {
for (auto& next_in_chain : cmd->next_chain) {
run_tail(next_in_chain, job->exit_code());
}
});
}
}
NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands) NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
{ {
NonnullRefPtrVector<Job> jobs_to_wait_for; NonnullRefPtrVector<Job> spawned_jobs;
for (auto& command : commands) { for (auto& command : commands) {
#ifdef SH_DEBUG #ifdef SH_DEBUG
@ -663,21 +695,19 @@ NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
if (!job) if (!job)
continue; continue;
spawned_jobs.append(*job);
if (command.should_wait) { if (command.should_wait) {
block_on_job(job); block_on_job(job);
if (!job->is_suspended())
jobs_to_wait_for.append(*job);
} else { } else {
if (command.is_pipe_source) { if (command.is_pipe_source) {
job->set_running_in_background(true); job->set_running_in_background(true);
jobs_to_wait_for.append(*job);
} else if (command.should_notify_if_in_background) { } else if (command.should_notify_if_in_background) {
job->set_should_announce_exit(true); job->set_should_announce_exit(true);
} }
} }
} }
return jobs_to_wait_for; return spawned_jobs;
} }
bool Shell::run_file(const String& filename, bool explicitly_invoked) bool Shell::run_file(const String& filename, bool explicitly_invoked)
@ -709,6 +739,9 @@ void Shell::block_on_job(RefPtr<Job> job)
if (!job) if (!job)
return; return;
if (job->is_suspended())
return; // We cannot wait for a suspended job.
ScopeGuard io_restorer { [&]() { ScopeGuard io_restorer { [&]() {
if (job->exited() && !job->is_running_in_background()) { if (job->exited() && !job->is_running_in_background()) {
restore_ios(); restore_ios();

View file

@ -195,6 +195,9 @@ private:
const Job* m_current_job { nullptr }; const Job* m_current_job { nullptr };
LocalFrame* find_frame_containing_local_variable(const String& name); LocalFrame* find_frame_containing_local_variable(const String& name);
void run_tail(RefPtr<Job>);
void run_tail(const AST::NodeWithAction&, int head_exit_code);
virtual void custom_event(Core::CustomEvent&) override; virtual void custom_event(Core::CustomEvent&) override;
#define __ENUMERATE_SHELL_BUILTIN(builtin) \ #define __ENUMERATE_SHELL_BUILTIN(builtin) \

View file

@ -1,13 +1,17 @@
#!/bin/sh #!/bin/sh
setopt --verbose
if test 1 -eq 1 { if test 1 -eq 1 {
# Are comments ok? # Are comments ok?
# Basic 'if' structure, empty block. # Basic 'if' structure, empty block.
if true { if true {
} else { } else {
echo "if true runs false branch"
exit 2 exit 2
} }
if false { if false {
echo "if false runs true branch"
exit 2 exit 2
} else { } else {
} }

View file

@ -23,69 +23,69 @@ test "$(echo yes)" = yes || exit 2
alias fail="echo Failure: " alias fail="echo Failure: "
# Redirections. # Redirections.
test -z "$(echo foo > /dev/null)" || fail direct path redirection test -z "$(echo foo > /dev/null)" || fail direct path redirection && exit 2
test -z "$(echo foo 2> /dev/null 1>&2)" || fail indirect redirection test -z "$(echo foo 2> /dev/null 1>&2)" || fail indirect redirection && exit 2
test -n "$(echo foo 2> /dev/null)" || fail fds interfere with each other test -n "$(echo foo 2> /dev/null)" || fail fds interfere with each other && exit 2
# Argument unpack # Argument unpack
test "$(echo (yes))" = yes || fail arguments inside bare lists test "$(echo (yes))" = yes || fail arguments inside bare lists && exit 2
test "$(echo (no)() yes)" = yes || fail arguments inside juxtaposition: empty test "$(echo (no)() yes)" = yes || fail arguments inside juxtaposition: empty && exit 2
test "$(echo (y)(es))" = yes || fail arguments inside juxtaposition: list test "$(echo (y)(es))" = yes || fail arguments inside juxtaposition: list && exit 2
test "$(echo "y"es)" = yes || fail arguments inside juxtaposition: string test "$(echo "y"es)" = yes || fail arguments inside juxtaposition: string && exit 2
# String substitution # String substitution
foo=yes foo=yes
test "$(echo $foo)" = yes || fail simple string var lookup test "$(echo $foo)" = yes || fail simple string var lookup && exit 2
test "$(echo "$foo")" = yes || fail stringified string var lookup test "$(echo "$foo")" = yes || fail stringified string var lookup && exit 2
# List substitution # List substitution
foo=(yes) foo=(yes)
# Static lookup, as list # Static lookup, as list
test "$(echo $foo)" = yes || fail simple list var lookup test "$(echo $foo)" = yes || fail simple list var lookup && exit 2
# Static lookup, stringified # Static lookup, stringified
test "$(echo "$foo")" = yes || fail stringified list var lookup test "$(echo "$foo")" = yes || fail stringified list var lookup && exit 2
# Dynamic lookup through static expression # Dynamic lookup through static expression
test "$(echo $'foo')" = yes || fail dynamic lookup through static exp test "$(echo $'foo')" = yes || fail dynamic lookup through static exp && exit 2
# Dynamic lookup through dynamic expression # Dynamic lookup through dynamic expression
ref_to_foo=foo ref_to_foo=foo
test "$(echo $"$ref_to_foo")" = yes || fail dynamic lookup through dynamic exp test "$(echo $"$ref_to_foo")" = yes || fail dynamic lookup through dynamic exp && exit 2
# More redirections # More redirections
echo test > /tmp/sh-test echo test > /tmp/sh-test
test "$(cat /tmp/sh-test)" = test || fail simple path redirect test "$(cat /tmp/sh-test)" = test || fail simple path redirect && exit 2
rm /tmp/sh-test rm /tmp/sh-test
# 'brace' expansions # 'brace' expansions
test "$(echo x(yes no))" = "xyes xno" || fail simple juxtaposition expansion test "$(echo x(yes no))" = "xyes xno" || fail simple juxtaposition expansion && exit 2
test "$(echo (y n)(es o))" = "yes yo nes no" || fail list-list juxtaposition expansion test "$(echo (y n)(es o))" = "yes yo nes no" || fail list-list juxtaposition expansion && exit 2
test "$(echo ()(foo bar baz))" = "" || fail empty expansion test "$(echo ()(foo bar baz))" = "" || fail empty expansion && exit 2
# Variables inside commands # Variables inside commands
to_devnull=(>/dev/null) to_devnull=(>/dev/null)
test "$(echo hewwo $to_devnull)" = "" || fail variable containing simple command test "$(echo hewwo $to_devnull)" = "" || fail variable containing simple command && exit 2
word_count=(() | wc -w) word_count=(() | wc -w)
test "$(echo well hello friends $word_count)" -eq 3 || fail variable containing pipeline test "$(echo well hello friends $word_count)" -eq 3 || fail variable containing pipeline && exit 2
# Globs # Globs
mkdir sh-test mkdir sh-test
pushd sh-test pushd sh-test
touch (a b c)(d e f) touch (a b c)(d e f)
test "$(echo a*)" = "ad ae af" || fail '*' glob expansion test "$(echo a*)" = "ad ae af" || fail '*' glob expansion && exit 2
test "$(echo a?)" = "ad ae af" || fail '?' glob expansion test "$(echo a?)" = "ad ae af" || fail '?' glob expansion && exit 2
glob_in_var='*' glob_in_var='*'
test "$(echo $glob_in_var)" = '*' || fail substituted string acts as glob test "$(echo $glob_in_var)" = '*' || fail substituted string acts as glob && exit 2
test "$(echo (a*))" = "ad ae af" || fail globs in lists resolve wrong test "$(echo (a*))" = "ad ae af" || fail globs in lists resolve wrong && exit 2
test "$(echo x(a*))" = "xad xae xaf" || fail globs in lists do not resolve to lists test "$(echo x(a*))" = "xad xae xaf" || fail globs in lists do not resolve to lists && exit 2
test "$(echo "foo"a*)" = "fooad fooae fooaf" || fail globs join to dquoted strings test "$(echo "foo"a*)" = "fooad fooae fooaf" || fail globs join to dquoted strings && exit 2
popd popd
rm -fr sh-test rm -fr sh-test
# Setopt # Setopt
setopt --inline_exec_keep_empty_segments setopt --inline_exec_keep_empty_segments
test "$(echo -n "a\n\nb")" = "a b" || fail inline_exec_keep_empty_segments has no effect test "$(echo -n "a\n\nb")" = "a b" || fail inline_exec_keep_empty_segments has no effect && exit 2
setopt --no_inline_exec_keep_empty_segments setopt --no_inline_exec_keep_empty_segments
test "$(echo -n "a\n\nb")" = "a b" || fail cannot unset inline_exec_keep_empty_segments test "$(echo -n "a\n\nb")" = "a b" || fail cannot unset inline_exec_keep_empty_segments && exit 2