Why ssh fails from crontab but succeeds when executed from a command line?

keychain solves this in a painless way. It’s in the repos for Debian/Ubuntu: sudo apt-get install keychain and perhaps for many other distros (it looks like it originated from Gentoo). This program will start an ssh-agent if none is running, and provide shell scripts that can be sourced and connect the current shell to this … Read more

Using conditional statements inside ‘expect’

Have to recomment the Exploring Expect book for all expect programmers — invaluable. I’ve rewritten your code: (untested) proc login {user pass} { expect “login:” send “$user\r” expect “password:” send “$pass\r” } set username spongebob set passwords {squarepants rhombuspants} set index 0 spawn telnet 192.168.40.100 login $username [lindex $passwords $index] expect { “login incorrect” { … Read more

bash double bracket issue

The problem lies in your script invocation. You’re issuing: $ sudo sh if_test.sh On Ubuntu systems, /bin/sh is dash, not bash, and dash does not support the double bracket keyword (or didn’t at the time of this posting, I haven’t double-checked). You can solve your problem by explicitly invoking bash instead: $ sudo bash if_test.sh … Read more

how to extract a substring in bash

Do use the expression {string:position:length} So in this case: $ str=”abcdefghijklm” $ echo “${str:0:5}” abcde See other usages: $ echo “${str:0}” # default: start from the 0th position abcdefghijklm $ echo “${str:1:5}” # start from the 1th and get 5 characters bcdef $ echo “${str:10:1}” # start from 10th just one character k $ echo … Read more

Assigning the result of ‘test’ to a variable

As others have documented here, using the string “true” is a red herring; this is not an appropriate way to store boolean values in shell scripts, as evaluating it means dynamically invoking a command rather than simply inspecting the stored value using shell builtins hardcoded in your script. Instead, if you really must store an … Read more