Expect: How to access variables and commands
I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.
/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}
send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}
The output shows that it hasnt got the value for item.
]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"
bash expect spawn
add a comment |
I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.
/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}
send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}
The output shows that it hasnt got the value for item.
]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"
bash expect spawn
Why did you escape the$
before runningls
and not foritem
?
– Inian
Nov 20 '18 at 13:20
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07
add a comment |
I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.
/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}
send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}
The output shows that it hasnt got the value for item.
]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"
bash expect spawn
I'm attempting to do a for loop so that i can read each file from the ls output inside my expect script.
/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
expect {
"*assword: "
}
send "_t00rhs!r"
expect {
"*]# "
}
send 'for item in "$(ls)" do sort -k1 -n ${item}; done r'
expect {
"*]# "
}
The output shows that it hasnt got the value for item.
]0;root@atddpvm13:~[?1034h[root@atddpvm13 ~]# can't read "item": no such
variable
while executing
"send 'for item in "$(ls)" do sort -k1 -n ${item}"
bash expect spawn
bash expect spawn
asked Nov 20 '18 at 13:17
GillGill
64
64
Why did you escape the$
before runningls
and not foritem
?
– Inian
Nov 20 '18 at 13:20
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07
add a comment |
Why did you escape the$
before runningls
and not foritem
?
– Inian
Nov 20 '18 at 13:20
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07
Why did you escape the
$
before running ls
and not for item
?– Inian
Nov 20 '18 at 13:20
Why did you escape the
$
before running ls
and not for item
?– Inian
Nov 20 '18 at 13:20
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07
add a comment |
1 Answer
1
active
oldest
votes
From the expect side:
- single quotes have no special meaning in expect
- use braces for non-interpolating quoting
- however the
r
sequence will not be interpreted in braces
So you get
send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"
From the shell side
- don't parse
ls
- don't use
for
to iterate lines
You want
send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393877%2fexpect-how-to-access-variables-and-commands%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the expect side:
- single quotes have no special meaning in expect
- use braces for non-interpolating quoting
- however the
r
sequence will not be interpreted in braces
So you get
send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"
From the shell side
- don't parse
ls
- don't use
for
to iterate lines
You want
send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"
add a comment |
From the expect side:
- single quotes have no special meaning in expect
- use braces for non-interpolating quoting
- however the
r
sequence will not be interpreted in braces
So you get
send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"
From the shell side
- don't parse
ls
- don't use
for
to iterate lines
You want
send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"
add a comment |
From the expect side:
- single quotes have no special meaning in expect
- use braces for non-interpolating quoting
- however the
r
sequence will not be interpreted in braces
So you get
send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"
From the shell side
- don't parse
ls
- don't use
for
to iterate lines
You want
send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"
From the expect side:
- single quotes have no special meaning in expect
- use braces for non-interpolating quoting
- however the
r
sequence will not be interpreted in braces
So you get
send {for item in $(ls); do sort -k1 -n "$item"; done}
send "r"
From the shell side
- don't parse
ls
- don't use
for
to iterate lines
You want
send "printf '%sn' * | sort -k1 -nr"
# or
send "ls -1vr"
answered Nov 20 '18 at 14:14
glenn jackmanglenn jackman
168k26147239
168k26147239
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393877%2fexpect-how-to-access-variables-and-commands%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why did you escape the
$
before runningls
and not foritem
?– Inian
Nov 20 '18 at 13:20
If i escape both, i get this ]0;root@server:~[?1034h[root@server~]# usage: send [args] string while executing "send 'for item in "$(ls)" do sort -k1 -n ${item}"
– Gill
Nov 20 '18 at 13:29
take a look at sexpect with which you can write Expect scripts with shell code only.
– pynexj
Nov 20 '18 at 14:07