0

I made a simple script to change the hostname and IP of an Alpine VM I have tried to install bash and run it, have also tried with sh I don't know what else to do or try. Can someone please help me I will leave the script and outputs.

script

#!/bin/bash
 
#hostname change
read -p "New hostname " newhostname
echo "$newhostname" > /etc/hostname
sed -i s/alpine-blank/$newhostname/g /etc/hosts
hostname -F /etc/hostname
hostname
 
echo ________________________________________
 
#Change IP to staic
read -p "New static IP " nsip
sed -i s/#//g /etc/network/interfaces
sed -i s/ip/$nsip/g /etc/network/interfaces
sed -i s/hm/$newhostname/g /etc/network/interfaces
sed -i 's/iface eth0 inet dchp/#iface eth0 inet dchp/g' /etc/network/interfaces
service networking restart
ip a
 
echo ________________________________________
 
#New motd
echo "IP: $nsip
 
Hostname: $newhostname
 
Alpine Linux 3" > /etc/motd
 
echo ________________________________________
 
#updates
apk update
apk upgrade
 
echo ________________________________________
 
#Reboot Y/N
while true; do
    read -p "confirm reboot. [Y/N]: " yn
    case $yn in
        [Yy]* ) reboot; break;;
        [Nn]* ) exit;;
    esac
done

script output ran with bash

alpine-blank:~# ls
setup.sh
alpine-blank:~# bash
alpine-blank:~# echo $0
bash
alpine-blank:~# ./setup.sh
bash: ./setup.sh: /bin/bash^M: bad interpreter: No such file or directory
alpine-blank:~#

script ran with sh and shebang defining /bin/sh

alpine-blank:~# ls
setup.sh
alpine-blank:~# sed -n 1p setup.sh
#!/bin/sh
alpine-blank:~# sh
alpine-blank:~# echo $0
sh
alpine-blank:~# ./setup.sh
sh: ./setup.sh: not found
alpine-blank:~# sh ./setup.sh
: not found line 2:
': bad variable nameme
: No such file or directory
: not found line 8: hostname
: not found line 9:
________________________________________
: not found line 11:
': bad variable name
: No such file or directorys
: No such file or directorys
: No such file or directorys
: No such file or directorys
'
BusyBox v1.34.1 (2022-04-04 10:19:27 UTC) multi-call binary.
 
Usage: ip [OPTIONS] address|route|link|tunnel|neigh|rule [ARGS]
 
OPTIONS := -f[amily] inet|inet6|link | -o[neline]
 
ip addr add|del IFADDR dev IFACE | show|flush [dev IFACE] [to PREFIX]
ip route list|flush|add|del|change|append|replace|test ROUTE
ip link set IFACE [up|down] [arp on|off] [multicast on|off]
        [promisc on|off] [mtu NUM] [name NAME] [qlen NUM] [address MAC]
        [master IFACE | nomaster]
ip tunnel add|change|del|show [NAME]
        [mode ipip|gre|sit] [remote ADDR] [local ADDR] [ttl TTL]
ip neigh show|flush [to PREFIX] [dev DEV] [nud STATE]
ip rule [list] | add|del SELECTOR ACTION
: not found line 20:
________________________________________
: not found line 22:
: not found line 29:
________________________________________
: not found line 31:
' is not an apk command. See 'apk --help'.
' is not an apk command. See 'apk --help'.
: not found line 35:
________________________________________
: not found line 37:
./setup.sh: line 41: syntax error: unexpected word (expecting "in")
alpine-blank:~#
  • 1
    Transcripts should be provided as text inside the body of the question itself, not screenshots or links. See [Why should I not upload screenshots of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551); for the reasons given therein, screenshots do not count towards providing a [mre] others can run to reproduce your problem and test answers (we need to be able to copy-and-paste; also, content only present in a screenshot isn't searchable, so makes it hard for anyone else to find your question and learn from its answers). – Charles Duffy May 18 '22 at 20:01
  • ...beyond that, for us to know we're contributing to a knowledge base that's useful for a long time to come, we want the question to be available to read and understand indefinitely, not just for whatever period of time a link remains good. – Charles Duffy May 18 '22 at 20:05
  • 1
    `/bin/bash^M: bad interpreter` means your script file uses `\r\n` newlines. Apply `dos2unix` to the file. This is also the cause of the other errors. – glenn jackman May 18 '22 at 21:47
  • Thank you for the edits. This is clearly the issue described in [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings), as Glenn referenced above. – Charles Duffy May 18 '22 at 22:04

1 Answers1

0

As you can see from the error message,

 /bin/bash^M: bad interpreter: No such file or directory

you have a Control-M (i.e. hexadecimal 0D) after bash. The message simply tells you that in the directory /bin, there is no file with this name. The solution is to remove this (and possible other ^M at the line ends) of your script. This can be done by

dos2unix setup.sh

For the future, I suggest that you configure your text editor to not insert these character at the end of the line.

user1934428
  • 19,864
  • 7
  • 42
  • 87