"if" statements of bash under mac os - macos

If there some difference between the bash of Mac OS and other linuxs'?
I wrote a simple bash script named "test.sh" like this:
#!/bin/bash
MYVAR=abc
if [ $MYVAR = abc ]; then
echo "ok"
fi
When I run it in terminal, some error occurs:
./test.sh: line 3: syntax error near unexpected token `then'
./test.sh: line 3: `if[ $MYVAR = abc ]; then'
then I delete the character ";" before "then" and run the script again, some infos occurs:
./test.sh: line 3: if[ abc = abc ]: command not found
ok
./test.sh: line 5: syntax error near unexpected token `fi'
./test.sh: line 5: `fi'
Could someone tell me what's wrong with my script?

Consider putting spaces into your file the way you put it in your example (if [).

[ is a command (same as test). It must be separated by spaces on both sides.

Related

bash script : syntax error near unexpected token `done'

I'm not sure why that error is coming.
Bash Script:
while read line do
grep "^$line$" sort-test1.txt >>matches.txt
done < sort-test2.txt
Error:
syntax error at line 5: `done' unexpected
Please tell me why this error is occuring.
If you wanted to obtain the lines in common in 2 text files. You can execute a simple shell command:
grep -F -x -f sort-test1.txt sort-test2.txt > matches.txt
I think that it is what you need.

Error in simple shell script prorgam

I am new to shell scripting and stuck in some syntax error in a simple program. I read an integer and compare it with some value to display the result
Please tell me how to rectify it.
#! /bin/bash
read n
if [ "$n" -le 12 ]
then
echo "a kid"
elif[ "$n" -lt 18 ]
then
echo "a teen"
else
echo "an adult"
fi
The error was:
./hello.sh: line 8: syntax error near unexpected token `then'
./hello.sh: line 8: `then'
You're missing a space between elif and [, which causes a parsing error later on.
For future reference, the shellcheck tool is a good way to diagnose errors like this one.

Error using arrays in shell script

I was testing a shell script in which arrays were used.
This is an example taken from tutorialspoint
#!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
But I am getting this error
test.sh: 3: test.sh: NAME[0]=Zara: not found
test.sh: 4: test.sh: NAME[1]=Qadir: not found
test.sh: 5: test.sh: NAME[2]=Mahnaz: not found
test.sh: 6: test.sh: NAME[3]=Ayan: not found
test.sh: 7: test.sh: NAME[4]=Daisy: not found
test.sh: 8: test.sh: Bad substitution
The link to the exact page is here
The shebang is wrong, this only works in specific shells, e.g. bash.
Simple. You just need to run as -
bash script_name.sh

Syntax error near unexpected token 'elif'

#!/bin/bash
if [ "$1" = "boot" ]
then
if [ -f /var/log/boot.log ]
then
echo /var/log/boot.log
elif [ -f /var/log/boot ]
then
echo /var/log/boot
fi
fi
This shows the output:
: command not foundline 8: GetLogfileName.sh: line 15: syntax error
near unexpected token `elif' 'etLogfileName.sh: line 15: `
elif [ -f /var/log/boot ]
What is going wrong here?
The garbled error message indicates your file has carriage returns before the newline. Did you edit your script on Windows? Either use your text editor to save the file without carriage returns or run the script through dos2unix (or perhaps d2u)
If you are using vi editor, set ":set ff=unix", save the file, and re-execute it.
This file format (ff) set command tells vi to use LF-only line endings when the file is saved.

Looping through files bash script

I am trying to run a simple loop through all files script but it's giving me the following error. The script is called test.sh and I am using Cygwin on Windows 7.
My script:
#!/bin/bash
FILES = "/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*"
for f in $FILES
do
echo "hello world"
done
The error is:
./test.sh: line 2: FILES: command not found
./test.sh: line 4: syntax error near unexpected token ``$'do\r''
./test.sh: line 4: ``do
Before running the script I converted all the files in folder to unix format using dos2unix command.
Try:
for f in `ls /bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*`; do echo "hello world"; done
Thanks!
Brandon
Collating other folks' answers into a single one.
You've two problems with this script:
The script still has Windows line endings (that's what the \r refers to; it's the character that Windows has in its line endings, but Unix doesn't). bcarlso pointed that one out. Run dos2unix over the script to sort it out.
When assigning variables in a bash script, you cannot have spaces around the = sign. scibuff caught that one.
The below gets interpreted as trying to run the command FILES (which doesn't exist) with the arguments = "/bowtie...".
FILES = "/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*"
Whereas the below is interpreted as assigning "/bowtie..." to the variable FILES:
FILES="/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*"
try
FILES=/bow.../*
for f in $FILES
do
echo "hello world"
done
i.e. no spaces around ' = '
Try to use the find-command
for i in `find /bow.../ -type f`
do
echo "hello world"
done
because ls will return directories too.
http://infofreund.de/bash-loop-through-files/
I have same the error:
./test.sh: line 2: FILES: command not found
./test.sh: line 4: syntax error near unexpected token $'do\r'' ./test.sh: line 4: do
I fixed it by change the ends line from 'CR LF' to 'LF' on the Cygwin when I run it on the windows

Resources