3

When running my shell script I get this error:

': not a valid identifiere 17: read: `

Here is my shell script:

#!/usr/bin/env bash
# Mr. Robot Install Wordpress Script
# Script is used for the following:
# add user to server
# change to new user home directory
# download latest version of wordpress
# unzip wordpress
# move all files up a directory level
# move up a directory level
# delete wordpress.zip
# remove wordpress folder
echo "/*****************************************************/"
echo "/************** HELLO MR. ROBOT **********************/"
echo "/*****************************************************/"
echo ".."
echo ".."
echo "Website URL" 
echo 'url: \r'
read -p $website 
echo 'User: \r' 
read -p $newuser
echo 'Pass: \r'
read -p $password 
echo "creating account......"
/scripts/wwwacct $website $newuser $password 0 x3 n n n  0 0 0 0 0 0
echo "Changing Directory....."
cd ~/home/$newuser/
echo "Getting Latest Version of Wordpress!"
curl -O http://wordpress.org/latest.tar.gz 
echo "Tarball Incoming!!"
tar xvzf latest.tar.gz
echo "removing tar file"
rm latest.tar.gz
echo "moving wordpress folders!"
cp -a ~/home/$newuser/public_html/wordpress/. ~/home/$newuser/public_html/
cd /home/$newuser/public_html/
echo "Part 01 Complete!!"
exit

I've tried to use different versions of the read line with -p or -e. Any help would be appreciated. I've even tried adding it on a separate line with input.

EDIT: Updated file to where it takes inputs, but issue is that the inputs are not being used through the rest of the script. Thus causing errors for directories not being found.

ZackAttack
  • 126
  • 12
  • 1
    I guess: Script has no shebang or is called with a different interpreter, not /bin/bash or wherever bash is installed. But read is not part of that interpreter. How do you invoke the script? And doesn't it exit, without exit statement (last line)? – user unknown Feb 11 '18 at 02:23
  • I run the script via command line: /scripts/mr-robot.sh – ZackAttack Feb 11 '18 at 02:29
  • 1
    Do two things before you proceed. First, run `dos2unix` on your script. Second, add a shebang of `#!/usr/bin/env bash`. Then start troubleshooting. – jww Feb 11 '18 at 02:32
  • 1
    The immediate cause of this is literally the **very first thing** in the "Before Asking About Problematic Code" section of [the StackOverflow `bash` tag wiki](https://stackoverflow.com/tags/bash/info). (You'd have a different bug with that fixed -- your values all being read into `REPLY` rather than the named variables due to the inappropriate expansions, but... well... that's a *different bug*, and also something that http://shellcheck.net/ would catch). – Charles Duffy Feb 11 '18 at 02:44
  • The marked duplicate question is useless; neither of the answers mention the actual problem (DOS/Windows line endings), or even any of the secondary problems (`$` when setting variables, missing shebang, missing double-quotes around variable references, etc). Maybe ["Why would a correct shell script give a wrapped/truncated/corrupted error message?"](https://stackoverflow.com/questions/31885409/why-would-a-correct-shell-script-give-a-wrapped-truncated-corrupted-error-messag) – Gordon Davisson Feb 11 '18 at 02:54
  • @CharlesDuffy Sounds good. – Gordon Davisson Feb 11 '18 at 02:56
  • @GordonDavisson Added a better answer to the dupe as https://stackoverflow.com/a/48727858/14122, and edited your suggestion into the dupe list here as well. – Charles Duffy Feb 11 '18 at 02:59
  • @ZacharyAdams, answers telling you to run `read website`, not `read $website`, long predate your edits. The `$` is your *other* problem, aside from the CRLFs. – Charles Duffy Feb 11 '18 at 03:03
  • 1
    @ZacharyAdams, ...btw, please consider running your script through http://shellcheck.net/ and fixing everything it finds. The quoting bugs, in particular, are likely to result in unwanted surprises down the line. – Charles Duffy Feb 11 '18 at 03:06

2 Answers2

1

Don't quote the variables names. read needs the name of the variable to assign to, not its value, which is what you get if you have a dollar sign $.

read -p 'website url: ' website 
read -p 'Username: ' newuser
read -p 'Password: ' password 

It looks like one of the variables holds \r, a carriage return. The error message that bash is trying to print is something like:

bash: ./script: line 17: read: `\r': not a valid identifier

But \r causes the cursor to go back to the beginning of the line, causing ': not a valid identifier to overwrite the beginning of the message.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

As mentioned above by John Kugelman, in case you have to check if you Input_file is having carriage returns then you could run following command:

cat -v Input_file

In case you find them then try to remove them from either of following cmmands:

tr -d '\r' < Input_file
OR
awk '{gsub(/\r/,"")} 1' Input_file

Or check if your system(box) has dos2unix utility you could use that also for removing these carriage returns.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93