My If statement in a function?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
coffee412
Level 8
Level 8
Posts: 2263
Joined: Mon Nov 12, 2012 7:38 pm
Location: I dont know
Contact:

My If statement in a function?

Post by coffee412 »

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???
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Ryzen x1800 Asus Prime x370-Pro 32 gigs Ram RX480 graphics
Dell PE T610, Dell PE T710
- List your hardware Profile: inxi -Fxpmrz
MeshCentral * Virtualbox * Debian * InvoiceNinja * NextCloud * Linux since kernel 2.0.36
WharfRat

Re: My If statement in a function?

Post by WharfRat »

You didn't end the if with a fi
lmuserx4849

Re: My If statement in a function?

Post by lmuserx4849 »

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 8
Level 8
Posts: 2263
Joined: Mon Nov 12, 2012 7:38 pm
Location: I dont know
Contact:

Re: My If statement in a function?

Post by coffee412 »

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
Dell PE T610, Dell PE T710
- List your hardware Profile: inxi -Fxpmrz
MeshCentral * Virtualbox * Debian * InvoiceNinja * NextCloud * Linux since kernel 2.0.36
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: My If statement in a function?

Post by Flemur »

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?
User avatar
coffee412
Level 8
Level 8
Posts: 2263
Joined: Mon Nov 12, 2012 7:38 pm
Location: I dont know
Contact:

Re: My If statement in a function?

Post by coffee412 »

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
Dell PE T610, Dell PE T710
- List your hardware Profile: inxi -Fxpmrz
MeshCentral * Virtualbox * Debian * InvoiceNinja * NextCloud * Linux since kernel 2.0.36
lmuserx4849

Re: My If statement in a function?

Post by lmuserx4849 »

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 8
Level 8
Posts: 2263
Joined: Mon Nov 12, 2012 7:38 pm
Location: I dont know
Contact:

Re: My If statement in a function?

Post by coffee412 »

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
Dell PE T610, Dell PE T710
- List your hardware Profile: inxi -Fxpmrz
MeshCentral * Virtualbox * Debian * InvoiceNinja * NextCloud * Linux since kernel 2.0.36
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: My If statement in a function?

Post by AndyMH »

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.... :)
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
Locked

Return to “Scripts & Bash”