Shell transmission lines to the function.

Hi all.
I have a problem. I have shell script:

Code:
#!/bin/sh
# example.

logFile() {
	if [ ${#} -eq 0]; then
		exit
	fi # end if().
	
	if [ ${1} = 'NULL' ]; then
		echo '' > $HOME/.mylog
	else
		echo ${1} >> $HOME/.mylog
	fi # end if().
} # end logFile function.


logFile NULL
logFile 'Hello world'

# end file.
If I send message how:
Code:
logFile 'It is message'

I have error:
Code:
[: Configuring: unexpected operator

If I send message how:
Code:
logFile ${msg='It is message'}
I do not have error. But my logfile has next string:
Code:
It
If I send message how:
Code:
logFile 'It_is_message'
or
Code:
logFile ${msg='It_is_message'}
All ok.

How can I fix it?
 
Quote your variables:
Code:
	if [ "${1}" = 'NULL' ]; then
		echo '' > $HOME/.mylog
	else
		echo "${1}" >> $HOME/.mylog
	fi # end if().

As suggestions, don't use tabs for indenting. Unless you have your editor set to show them as less than 8 spaces, I guess. Also, that comment on the "fi" is visual noise that makes the code harder to read.

Code:
  if [ "${1}" = 'NULL' ]; then
    echo '' > $HOME/.mylog
  else
    echo "${1}" >> $HOME/.mylog
  fi
 
Yes, thanks, it works.

Originally Posted by wblock.
As suggestions, don't use tabs for indenting. Unless you have your editor set to show them as less than 8 spaces, I guess. Also, that comment on the "fi" is visual noise that makes the code harder to read.

In Windows I used Notepad + + and installed instead of TAB - 4 space. In FreeBSD, I use the vi editor. When you start the vi editor, I ask:
Code:
: set tabstop = 4

The scenario that presented higher - I typed it right in your browser. Because not care about the number of indents. Just do not care about design.

But, thank you very much.
 
Back
Top