My If statement in a function?

Forum rules
Before you post please read how to get help
Post Reply
User avatar
coffee412
Level 5
Level 5
Posts: 820
Joined: Mon Nov 12, 2012 7:38 pm
Location: Indiana, USA
Contact:

My If statement in a function?

Post by coffee412 » Fri Jun 15, 2018 6:28 pm

This doesnt work. What am I doing wrong?

Code: Select all

function test { 
read -p ' Testing your typing ' type
if [ $type = "y" ]; then
echo "You typed the word " $type
else
echo "You typed some other word"
}

test
syntax error: unexpected end of file

Something wrong with my "If" statement. Seems I cannot use one in a function???
Ryzen x1800 Asus Prime x370-Pro 32 gigs Ram RX480 graphics
IceWarp * Samba AD * Mint 18.1 * RAID 1/5 * OpenVPN * Linux since kernel 2.0.36

User avatar
WharfRat
Level 20
Level 20
Posts: 11371
Joined: Thu Apr 07, 2011 8:15 pm

Re: My If statement in a function?

Post by WharfRat » Fri Jun 15, 2018 6:32 pm

You didn't end the if with a fi
ImageImage

lmuserx4849
Level 5
Level 5
Posts: 788
Joined: Wed Dec 17, 2014 2:55 am

Re: My If statement in a function?

Post by lmuserx4849 » Fri Jun 15, 2018 6:50 pm

coffee412 wrote:
Fri Jun 15, 2018 6:28 pm
..
syntax error: unexpected end of file
...
This error usually means some sort of bash syntactical error (thus the "syntax error" part of the message :-)).

I'm on bash 4.3.11 (lm 17.3), and when I executed that snippet of code, I got "line 11: syntax error near unexpected token `}'". That would be an indicator to start there and look backwards. Just remember bash parses each word, separated by whitespace with quotes having a special roll, and it'll keep looking through the source file until it finds what it expects or end of file :-)

===> bash --version
GNU bash, version 4.3.11

User avatar
coffee412
Level 5
Level 5
Posts: 820
Joined: Mon Nov 12, 2012 7:38 pm
Location: Indiana, USA
Contact:

Re: My If statement in a function?

Post by coffee412 » Fri Jun 15, 2018 7:32 pm

Believe it or not, I think this is Murphy's Law --> You look for ever to find an answer and then decide to post about it. Then right after that you find the answer.

At the end of the whole if statement I was missing "fi".

So, It should look like this:

Code: Select all

function test { 
read -p ' Testing your typing ' type
if [ $type = "joe" ]; then
echo "You typed the word " $type
else
echo "You typed something else"
fi
}

test
Take the 'fi" (end) statement out and bash goes peanuty. :)
Ryzen x1800 Asus Prime x370-Pro 32 gigs Ram RX480 graphics
IceWarp * Samba AD * Mint 18.1 * RAID 1/5 * OpenVPN * Linux since kernel 2.0.36

User avatar
Flemur
Level 15
Level 15
Posts: 5805
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: My If statement in a function?

Post by Flemur » Fri Jun 15, 2018 8:12 pm

It doesn't like spaces:

Code: Select all

$ test
 Testing your typing hello there
bash: [: too many arguments
You typed something else
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

User avatar
coffee412
Level 5
Level 5
Posts: 820
Joined: Mon Nov 12, 2012 7:38 pm
Location: Indiana, USA
Contact:

Re: My If statement in a function?

Post by coffee412 » Fri Jun 15, 2018 8:29 pm

Yes, That was an example of the problem I was having with my if/then statements. I have a rather large script for administering a User Database on a backup system. Its getting quite long. Basically it makes it easy for an employee to work with customers and assign them a qouta for storage and set them up for samba. Its a labor of love here :)
Ryzen x1800 Asus Prime x370-Pro 32 gigs Ram RX480 graphics
IceWarp * Samba AD * Mint 18.1 * RAID 1/5 * OpenVPN * Linux since kernel 2.0.36

lmuserx4849
Level 5
Level 5
Posts: 788
Joined: Wed Dec 17, 2014 2:55 am

Re: My If statement in a function?

Post by lmuserx4849 » Fri Jun 15, 2018 9:27 pm

Just for fun I'd write:

Code: Select all

#!/bin/bash
set -u

function test() {
  local -- type=''
  local -- rc=0

  while [[ -z "${type}" ]]; do
    read -rp 'Testing your typing [y|other]: ' type
  done

  # rhs can be a string (quotes), glob pattern (no quotes), or a regular expression (=~)
  if [[ "${type}" == 'y' ]]; then
    printf -- 'You typed the word: %s\n' "${type}"
  else
    printf -- 'You typed some other word: %s\n' "${type}"
    rc=1
  fi

  return "${rc}"
}

test && printf -- 'Return: %d\n' "${?}" ||  exit 
There is some debate, if you have a literal string, that single quotes are "faster", because bash doesn't have to parse them.
This should work with single brackets too, but I prefer double. I like to define variables (local or declare), and also use set -u. A lot of times, set -x helps with debugging, especially with the single brackets. You can see what happens with the expansion. 99% of the time, quote expansions.

This is a helpful resource - Unofficial Bash FAQ. Even if you know certain things, it'll show you alternatives or best practices in some cases.

In the above example, you'll notice $type is local. You could just declare it global, or you could do something with redirection like:

# within the function
...
printf -- 'You typed the word: %s\n' "${type}" >&3
printf -- '%s' "${type}"
...

# main: use command substitution & capture the value
exec 3>&1
type="$(test)"
exec 3>&-

Redirection is really powerful. I always have to think about it for awhile :-)

User avatar
coffee412
Level 5
Level 5
Posts: 820
Joined: Mon Nov 12, 2012 7:38 pm
Location: Indiana, USA
Contact:

Re: My If statement in a function?

Post by coffee412 » Sat Jun 16, 2018 5:46 am

Hey, Thank you.

I will sit down and digest this in a little bit. Just rolled out of bed.

I have done bash before but its been a long time. Now that I have a server running (ml350e) that I need to incorporate some complex commands into a simple script :)

Thank you again!
Ryzen x1800 Asus Prime x370-Pro 32 gigs Ram RX480 graphics
IceWarp * Samba AD * Mint 18.1 * RAID 1/5 * OpenVPN * Linux since kernel 2.0.36

User avatar
AndyMH
Level 4
Level 4
Posts: 429
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: My If statement in a function?

Post by AndyMH » Sat Jun 16, 2018 2:07 pm

Simple, but I have a lot of sympathy with this. You've got bash, a bit of python, throw in some pascal, then some VB, etc. And those are just the ones I use (with variable frequency). Now can I remember the syntax for an if or a case statement or a do while or is it while do.... :)
Thinkpad T430 i7-3632 Cinnamon 18.3, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

Post Reply

Return to “Scripts & Bash”