how to escape unusual/uniq characters from expect scripts?
Using different Tcl quotes will work
expect # {
# send text verbatim here
send {perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts}
# interpret backslash sequence as carriage return here
send "\r"
}
expect + how to identify if expect break because time out?
To do sensible things on timeout, you need to tell expect what should happen:
set timeout 10
expect {
")?" { send "yes\r" ; exp_continue }
"word:" { send "$PASS\r" }
timeout { puts "timed out during login"; exit 1 }
}
set timeout -1 ; # Infinite...
expect ">" { send "hostname\r" }
expect ">" { send "exit\r" }
expect eof
exit
Notice above how I use exit 1
when I hit an error. Your shell will be able to pick that up through $?
, etc. (Without the 1
argument, the exit
command will cause the script to terminate “successfully”; the same happens if you drop off the bottom of the script.)
expect + how to identify if expect break because time out?
To do sensible things on timeout, you need to tell expect what should happen:
set timeout 10
expect {
")?" { send "yes\r" ; exp_continue }
"word:" { send "$PASS\r" }
timeout { puts "timed out during login"; exit 1 }
}
set timeout -1 ; # Infinite...
expect ">" { send "hostname\r" }
expect ">" { send "exit\r" }
expect eof
exit
Notice above how I use exit 1
when I hit an error. Your shell will be able to pick that up through $?
, etc. (Without the 1
argument, the exit
command will cause the script to terminate “successfully”; the same happens if you drop off the bottom of the script.)
reading a variable from the user in an expect script
The "expect" way to do that is:
send_user "enter unique id: "
expect_user -re "(.*)\n"
set test $expect_out(1,string)
send_user "you said $test\n"
Since expect extends tcl, you could use:
puts -nonewline "enter unique id: "
flush stdout
gets stdin test
puts "you said $test"
Additionally, you'll get an error for mkdir
-- that's an external command. You can do one of:
exec mkdir -p /evoting_test/$test # invoke the external command
file mkdir /evoting_test/$test # use the builtin
See http://tcl.tk/man/tcl8.6/TclCmd/file.htm
Pass variable in list and expect its literal value
Braces prevent substitution of variables, so use double quotes instead:
set results " address contact $testString "
puts $results
# address contact John A. Smith, Mobile:001-445-4567-0987, Pin-556789
Or use subst
to force the substitution afterwards:
set results { address contact $testString }
set results [subst $results]
puts $results
# address contact John A. Smith, Mobile:001-445-4567-0987, Pin-556789
Or if you actually mean to have a list where the first element is address
, the second contact
and the third being $testString
's value, then you can use list
, except the output will look different:
set results [list address contact $testString]
puts $results
# address contact {John A. Smith, Mobile:001-445-4567-0987, Pin-556789}
But that way, you can get testString
back if you do something like lindex $results 2
, whereas if you used any of the earlier methods, you would get only the first word of testString
, that is, John
.
Is it possible to make a foreach loop for every character in a string?
You need to explicitly break the string into a list of characters:
foreach char [split $string ""] {...
How to loop over jq unique array in bash?
Works with change ' ' to '_' and back
messages=$( jq -rc '[.[].message] | unique | @sh' <<<"${commits}" )
messages="${messages// /_}"
messages=(${messages//"'_'"/"' '"})
echo "messages: ${messages[@]//_/ }"
for message in "${messages[@]//_/ }"
do
echo " message: $message"
done
Or like this
IFS=$'\n' messages=( $(jq -rc '.[].message' <<<"${commits}") )
printf "messages: "; printf "'%s' " "${messages[@]}"; echo
printf " message: '%s' \n" "${messages[@]}"
And we could do something like that
IFS=$'\n'
authors=($(jq -rc '.[].author.name' <<<"${commits}"))
messages=($(jq -rc '.[].message' <<<"${commits}"))
printf " authors | "; printf "'%s' " "${authors[@]}" ; echo
printf " author | '%s' \n" "${authors[@]}"
echo "---------+---------"
printf "messages | "; printf "'%s' " "${messages[@]}"; echo
printf " message | '%s' \n" "${messages[@]}"
To output like this
authors | 'Chris' 'John' 'John'
author | 'Chris'
author | 'John'
author | 'John'
---------+---------
messages | 'commit message 1' 'commit message 2' 'commit message 3'
message | 'commit message 1'
message | 'commit message 2'
message | 'commit message 3'
Related Topics
R Programming - Submitting Jobs on a Multiple Node Linux Cluster Using Pbs
Linux Kernel: Copy_From_User - Struct with Pointers
Bash: How to Perform Arithmetic on Numbers in a Pipe
How to Get Around the Linux "Too Many Arguments" Limit
Dotnetcore: Cross Platform Version of Getinvalidfilenamechars
Replace Line with Space and Backslash with a String Containing Spaces
One-Liner: Print All Lines Except the Last 3
Linux Perf Record: Difference Between Count (-C) and Frequency (-F) Options
Compile Git for 32-Bit Linux on Shared Hosting
Why Do I Get /Etc/Cups Conflicts Between Attempted Installs in Yocto
Asp Net Core Linux Err_Connection_Refused
Knowing If a Remote Port Forward Was Successful
Export_Symbol in Kernel Module | Undefined Symbol During Insmod
Terminate an Ongoing Qprocess That Is Running Inside a Qthread