./script: line 1: #!/bin/bash: No such file or directory enter ip
>>> : 10.000

10.00 0 ./script: line 8: [0!=.]: command not found ./script: line 13: =: command not found


echo "enter ip"
read -p ">>> : " ipaddr
echo ${temp}        
echo ${ipaddr%?}                    #display whole string except last character
echo ${ipaddr:(-1)}                 #display last character in string
while [${ipaddr:(-1)}!="."]     #loop till ${ipaddr} last == "."
    ${cur} = ${ipaddr::-1}
    echo $cur
done                                    #remove last char if != "." 
$temp = ${ipaddr}${var}         #add string ${var} to end of ${ipaddr}
echo ${temp}
  • 1
    while [ ${ipaddr:(-1)}!="." ] fixes the first error. Note the extra space after [ and before ] – DavidPostill Oct 20 '15 at 22:02
  • thanks for the response, any reason why these spaces are necassary? – CybeX Oct 20 '15 at 22:09
  • Because the bash condition syntax is defined that way. See my answer messaging.sh: line 29: [: missing `]' for a nice link for you to read. – DavidPostill Oct 20 '15 at 22:13
  • I've no idea how to fix the second error. It is not very clear what you are trying to do, and I'm not a bash expert. – DavidPostill Oct 20 '15 at 22:20
  • what i am trying to accomplish, stated by comments, is read in ip address from user input, remove the last digits till i have the form "x.x.x." then add ${var} to itso it becomes x.x.x.0/24 – CybeX Oct 20 '15 at 22:26

Since I do not have enough rep to add these as comments to the existing answers, I will just write a new answer.

  • Assignment of values to a variable in BASH uses just the variable's name, without the $-sign. So use foo="something", and NOT $foo="something".

  • [ is an abbreviation of the program/command test. Commands take their arguments seperated by spaces. By omitting the space after [, you are telling BASH to interpret the whole set of characters as one word/string. While with the space, the rest of the line gets passed as an argument to the command test. When invoked with [ however, test expects one of its arguments to be ], and will complain if that is missing. So use if [ -f /etc/motd ] and NOT if [-f /etc/motd]

| improve this answer | |
  • Welcome to stackexchange! Good job on your first answer; I think it's good that you explained some basics of the language (bash) rather than just fixing the problems with the specific code. :) Check out the tour page—superuser.com/tour—comments are for comments, not answers, so posting this as an answer is not a bad thing at all. – Wildcard Oct 21 '15 at 10:33
  • These would have served fine as a comment on the below answer by Archenar, that does fix the code and give a brief explanation. I just wanted to further clarify. (finally got the rep to comment ;) ) – DOK Mar 2 '18 at 8:58

change revelant lines to

while [ ${ipaddr:(-1)} != "." ]     #loop till ${ipaddr} last == "."
    echo $cur
done                                    #remove last char if != "." 
temp=${ipaddr}${var}         #add string ${var} to end of ${ipaddr}
  • in test, you must use space ( because [${ipaddr:(-1)}!="."] is interpreted by bash as a single word) Space after [ and before ] and a space on each side of the operator !=.
  • in variable assignment, do not use ${var} on the left hand side, but directly var=EXPR. (On the right hand side you use a dollar sign $ to refer to the value of the variable.)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.