Why is my expect script failing on line 1? [closed] - bash

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
The very first line of my expect script fails. Here are the entire contents of my script and it fails:
#!/usr/bin/expect -f
And it fails right off the bat with
": no such file or directory
as my response. Expect is in fact installed and is located in /usr/bin/ and I am running this from root. I have no extra spaces or lines before the # sign either. Of course there was more to the script originally but it fails way before it gets to the good stuff.

Tried it and here is the result: /usr/bin/expect^M: bad interpreter
Is it possible that there's a Windows newline (the ^M) in there that's confusing the script? You can try od to see what newline character(s) is after the expect and tofromdos or an editor (e.g. emacs in hexl-mode) to remove it. See the man pages for more info.

I had this issue and found I didn't have the expect interpreter installed! Oddly enough, if you ran the command in the shell it worked. However, through a shell script I got this error:
/usr/bin/expect: bad interpreter: No such file or directory
I fixed it by simply installing the Expect interpreter. The package name that was chosen was: expect libtcl8.6
Just run:
sudo apt-get install expect

Your line endings are wrong. Shove it through dos2unix or tr -d '\r'.

I don't really know expect, to be honest, but when I run that on my system it "works" fine. Nothing happens, but that's what I'd expect. I don't get any error message. According to the man page,
#!/usr/bin/expect -f
is the correct way to start your script. Expect then slurps up the script you are executing as the cmdfile.
The way I got it to reproduce the problem was to actually put a ^M at the end of the line instead of a normal newline (saw Bert F's response and that prompted me to try it). I'm sure vim's :set list command will show any odd characters.

If you observe the error, there was a windows newline character, that is added because its copied from windows machine via mail or winscp. So to avoid this error copy the script using scp linux to linux and execute the script.

Related

echo, printf not work in script when run it as command [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I test the script in WSL2
Here is my script:
#!/usr/bin/bash
echo "testing..."
printf "testing..."
It work fine if I run like
bash test
source test
. test
But it output nothing if I add the path the script located in to PATH and run
test
Why and how can I fix it?
test is a bash built-in. POSIX systems will also have a test executable.
When you enter a command without specifying a path to the executable, bash will first check if the command is one of its built-in commands before searching for the executable in the PATH. If the command matches the name of one of the bash built-ins, it will run the built-in.
If you still want to run your script without specifying its path, there are two ways to do it:
Recommended: Rename your file, and then run it with its new name (your script file needs to have its executable permission bit(s) set).
Make sure your script has its file permissions set so that it is executable, make sure your PATH is set up so that your test will be found before the system's test, and then run env test to run your script. env will search your PATH to find your test executable, and then it will execute it.
Ultimately, option 2 is not recommended, because it can be brittle to reorder your PATH, and it can be confusing (for you and for others) to have a second test binary on your system.

Command runs in terminal, but doesn't work in script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
On Linux Mint, the command "timedatectl" works as expected from terminal, but doesn't work from the bash script. I think I made some very stupid mistake, as I'm a complete newbie in scripting, but can't find it myself.
#!/bin/bash
timedatectl set-timezone UTC
timedatectl status
prints out
Failed to set time zone: Invalid time zone 'UTC'
Unknown operation status
The same commands work just fine from terminal, and setting the timezone and printing out times and time settings.
UPD:
which timedatectl
prints out
/usr/bin/timedatectl
, but adding this full path to the calls in the script doesn't change the output.
Trying to set another timezone succeeds from the terminal (timezone changes), but fails from the script. (Failed to set time zone: Invalid time zone 'Europe/Paris')
echo $PATH returns
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
The system doesn't ask for password to execute these commands from the terminal.
UPD2: I've found what was wrong, thanks to the comment by Whydoubt.
I was calling the script like
bash script1.sh
but after the comment I tried to call it like
./script1.sh
and got
bash: ./script1.sh: /bin/bash^M: bad interpreter: No such file or directory
So I realised the problem was with the newline symbol (the original script was created on a Windows machine with Notepad++). After retyping the script with xed, everything started to work as expected.
The output of uname -a, hostname, hostid and hostnamectl, who, groups, stat /usr/share/zoneinfo and stat /usr/share/zoneinfo/UTC are the same from the console and from a script. /usr/share/zoneinfo/UTC is accessible from the script. I think I don't need to show the output as it's rather lengthy and my mistake is already found.
Should I write an answer myself to make the question answered and completed?

Why do double quotes break the ssh statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I am trying to ssh into my amazon server. I have two ways of doing it, one works and one doesn't, and I'm not sure why the second one does not work.
First way:
ssh -i path-to-pem-file username#public-ip
This works.
Second way:
ssh -i "path-to-pem-file" username#public-ip
This results in "Warning: Identity file "path-to-pem-file" not accessible".
Both of the above commands are run from the terminal on Mac OSX. Why do the double quotes break the statement? thanks.
If your using shell expansion or other special characters their special meanings will not be interpreted when quoted. They are considered literal values.
You can replicate this with the ~ or special character for $HOME
Doesnt work
ssh -i "~/mypemkey.pem" ec2-user#somehost
Works
ssh -i ~/mypemkey.pem ec2-user#somehost
Essentially the ssh application is trying to find a literal file path ~/ instead of /Users/someuser/ when expanded.
Want to see it in action under the hood.... test it!
Create a simple bash script
echo "echo \$1" > test.sh
Execute it
/bin/bash test.sh ~/Desktop
outputs: /Users/phpisuber01/Desktop
/bin/bash test.sh "~/Desktop"
outputs: ~/Desktop

what does the error message "[[: not found" mean [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
I am debugging a bash shell script, and I am getting this error message:
[[: not found
The line number it points to is the end of my outer do loop.
Any ideas?
Thanks!
Edit: here is the script: https://github.com/stephenh/git-central/blob/master/server/post-receive-hudson
The [[ is used in BASH as a builtin test condition. However, it doesn't work in regular Bourne shell that many systems default to when running things like cronjobs, etc.
Are you putting the shebang (#! /bin/bash) as the first line of your shell scripts? Is this a cronjob? Can you print out the value of $RANDOM (Bash will print out a value, Bourne will not)?
Show us the program that's giving you this problem, and tell us about the system it's running on (Linux? Solaris? Intel? Cygwin?) maybe we can figure it out.

Shell script and CRON problems [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've written a backup script for our local dev server (running Ubuntu server edition 9.10), just a simple script to tar & gzip the local root and stick it in a backup folder.
It works fine when I run :
$ bash backups.sh
but it wont work when I run it through crontab.
59 23 * * * bash /home/vnc/backups/backup.sh >> /home/vnc/backups/backup.log 2> $1
I get the error message
/bin/sh: cannot create : nonexistent
The script makes the tar.gz in the folder it is running from (/home/user1), but then tries to copy it to a mounted share (/home/backups, which is really 192.168.0.6/backups) from a network drive, via using fstab.
The mounted share has permissions 777 but the owner and group are different to those running the script.
I'm using bash to run the script instead of sh to get around another issue I've had in the past with "bad substitution" errors
The first 2 lines of the file are
! /bin/bash
cd /home/vnc/backups
I'm probably not supplying enough information to fully answer this post yet but I can post more information as necessary, but I don't really know where to look next.
The clue is in the error message:
/bin/sh: cannot create : nonexistent
Notice that it says "sh". The Bourne shell doesn't support some features that are specific to Bash. If you're using Bash features, then you need to tell Bash to run the script.
Make the first line of your file:
#!/bin/bash
or in your crontab entry do this:
* * * * * /bin/bash scriptname
Without seeing your crontab entry and your script it's hard to be any more specific.
Perhaps the first thing you should do in your backups.sh is insert a cd /home/user1. crond may execute your script from a different directory than you think it does, and forcing it to use the same directory regardless of how it is executed could be a good first start.
Another potentially useful debugging step is to add id > /tmp/id.$$ or something like that, so you can see exactly which user account and groups are being used to run your script.
In crontab, just change 2>$1 to 2>&1. I've just done one myself. Thank you Dennis Williamson.

Resources