I want to add .java to my string if it doesn't have it already. How can I make this work with an if structure? This is what I tried:

  6 echo geef eens je naam van je java programma?
  8 read naam
  9 firstletter=${naam:0:1}
 11 naamm="${naam^}"
 12 echo $naamm
 14 add=".java"
 16 if [ ${naamm:(-5)} != ".java" ]
 17 then
 18         naamm=$naamm$add
 19         echo $naamm
 20 else
 21         #nothing
 22 fi
  • 4
    Could you take a peek at the editing help, especially the first part about code blocks, and reformat the text with those? (hint, select the text and hit Ctrl-K). I don't really think the line numbers are necessary, but you could do well to add the error message(s) you get, if any. – ilkkachu Dec 23 '17 at 10:34
  • 1
    A then or an else clause can't be empty or contain only a comment (you'll get an error like syntax error near unexpected token 'fi'). If you really need an empty clause with only a comment, use the null/no-op statement :. e.g. if foo then; bar ; else : ; fi (note there must be a newline between the else and the : so this won't work as a one-liner). See unix.stackexchange.com/a/133976/7696 – cas Dec 23 '17 at 11:25
  • 1
    @florian, which shell are you using? bash? – glenn jackman Dec 23 '17 at 14:32
  • @glennjackman i am using bash and openjdk-8-jdk but i dont know what i need this openjdk-8-jdk for – florian de wulf Dec 23 '17 at 14:38
  • @glennjackman I have one more question after that if u dont mind, now I want to write something in that file I just made "public class $naamm {" and "public main..." but i cant seem to find how to do this.. do u maybe now how to do this? thanks – florian de wulf Dec 23 '17 at 19:34

Several possibilities:

if ! [[ $naamm =~ \.java$ ]]




if [ "${name: -5}" = '.java' ]

additional question

Content can be written to a file like this:

echo "public class $naamm {
public main..." >file

For bigger contents this approach is easiert:

cat <<\EOT >file
public class $naamm {
    public main...
| improve this answer | |
  • 1
    or, [[ $naam != *.java ]] -- requires bash or ksh – glenn jackman Dec 23 '17 at 14:31
  • this last one worked for me thanks a lot, does sombody now know how I could make a file with the name I readed if the file doesnt exists already? – florian de wulf Dec 23 '17 at 14:53
  • @HaukeLaging oh i am sorry, and thanks btw, but this line gave me the error "touch: binary operator expected" – florian de wulf Dec 23 '17 at 15:48
  • @floriandewulf Sorry, typo: test -f "$name" || touch "$name" – Hauke Laging Dec 23 '17 at 17:22
  • @HaukeLaging cool that worked but can i also put that in an if structure so I can give an error if the name already exists? – florian de wulf Dec 23 '17 at 19:19

You can use grep to test if string has pattern and then append it or pass.

vars=(String.obj String.java)

for v in "${vars[@]}"; do
    if ! echo "${v}" | grep -q "\.java$"; then
        echo "${appended}"
| improve this answer | |
  • 2
    Put a bang ! in front of the command (if ! echo ...) and throw out the : and else lines. – B Layer Dec 23 '17 at 11:23
  • or use if grep -qv "\.java$" <<< "$v" ; then ... – ctx Dec 23 '17 at 14:33

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.