Error using arrays in shell script - shell

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

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.

Why would a bash script produce the error "command not founde"?

What is wrong with the 5th line in this script ( I have included the snippet that gives me the error and the actual error is listed in the bottom after the code and a link to complete script)?
#! /bin/bash
INSTALLDIR=/usr/local/mapguideopensource
CLEAN_FLAG=0
while [ $# -gt 0 ]; do # Until you run out of parameters...
case "$1" in
-prefix|--prefix)
INSTALLDIR="$2"
shift
;;
-clean|--clean)
CLEAN_FLAG=1
shift
;;
-help|--help)
echo "Usage: $0 (options)"
echo "Options:"
echo " --prefix [installation directory]"
echo " --clean [clean all objects and binaries in Oem]"
echo " --help [Display usage]"
exit
;;
esac
shift # Check next set of parameters.
done
This is the error i get when i run this bash script on linux (REHL5) :
: command not founde 4:
: command not founde 8:
: command not founde 8:
: command not founde 12:
MapGuide Open Source build script for OEM components
'/build_oem.sh: line 17: syntax error near unexpected token `in
'/build_oem.sh: line 17: ` case "$1" in
Please note, that the line number above corresponds to the actual script i am running (i have included a link to that script below)
The original script i am running
From the errors, I'm pretty sure you have carriage returns (aka CR or ^M) at the end of the lines. Windows/DOS text files have carriage return AND linefeed at the end of each line, but unix programs (like bash) just expect a linefeed, and get horribly confused if there's a CR as well. The giveaway is error messages like:
: command not founde 4:
What this really is is ./build_oem.sh: line 4: ^M: command not found, but the carriage return makes the terminal go back to the beginning of the line, and write the end of the message over the beginning of the message:
./build_oem.sh: line 4:
: command not found
|
V
: command not founde 4:
To fix the script, use dos2unix to convert it to proper unix format, then switch to a text editor that saves in unix format.
What choroba says, but also note that your shebang has to be on the first line (which it is not), otherwise it is useless since it's just a plain comment then and it won't necessarily execute under bash.
In the original script, lines 4 and 8 are empty. There is probably some invisible control character on the lines. Try xxd build_oem.sh.

"if" statements of bash under mac os

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.

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

Why does this simple bash code give a syntax error?

I have the following bash code, which is copied and pasted from "bash cookbook" (1st edition):
#!/bin/bash
VERBOSE=0;
if [[ $1 =-v ]]
then
VERBOSE=1;
shift;
fi
When I run this (bash 4.0.33), I get the following syntax error:
./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ $1 =-v ]]'
Is this as simple as a misprint in the bash cookbook, or is there a version incompatibility or something else here? What would the most obvious fix be? I've tried various combinations of changing the operator, but I'm not really familiar with bash scripting.
Bash uses spaces to tokenise scripts. The line:
if [[ $1 =-v ]]
should be:
if [[ $1 = -v ]]

Resources