So I have 1 bash scripts,
findFungible.sh
#!/bin/bash
for file in $*;
for word in $(cat $file);
if [ $word == Fungible ];
echo Fungible found
fi
done
done
Which should be checking files if they contain the word fungible.
It's pretty much verbatim out of my lecture example.
So if I run it with bash findFungible.sh
I get:
findFungible.sh: line 2: syntax error near unexpected token "$\r''
'indFungible.sh: line 2: 'for file in $*;
So I think it has something to do with windows putting in extra line \r characters or something. As there is a \r after $.
Then if I run it with sh findFungible.sh
I get:
findFungible.sh: 2: findFungible.sh Syntax error: word unexpected (expecting "do")
Any help would be great thanks.
As someone mentioned in a reply, you have syntax errors in this, meaning, you are missing "do's" and "then's".
#!/bin/bash
for file in $*; do
for word in $(cat $file); do
if [ $word == Fungible ]; then
echo Fungible found
fi
done
done
And yes, like mentioned in the reply, bash is very sensitive to white spaces, new lines and quotes, I'm not getting into too much detail there.
Related
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.
I am creating a shell script which takes a filename as input so i am passing the file name like
A(01).txt
so suppose I want to measure the size of files it show the error
bash: syntax error near unexpected token `('
I have just written
#!/bin/sh
if [ $# -eq 2 ]
then
FILE1="$1"
FILESIZE1=$(stat -c%s "$FILE1")
echo $FILESIZE1
fi
I am already adding " " to handle space in filename but why the parenthesis are not handled?
Thanks in advance.
Your filename has special characters. Either you have to change the filename with no special characters OR if you don't have permissions to change the file name then pass the filename to the script like <your_script> A\(01\).txt or <your_script> "A(01).txt"
I have a main folder within which there are a number of folders, each containing a number of text files. I need to run a program on all of these text files. So far I have the following bash script which throws me a syntax error when I try to execute:
#!/bin/bash
for dir in mainfolder/*
for file in ${dir}/*.txt
do
echo “${file}”
./myprogram ${file}
done
done
The error I get is:
./myscript: line 5: syntax error near unexpected token for'
./myscript: line 5:for file in ${dir}/*.txt'
You forgot the first do.
#!bin/bash
for dir in mainfolder/*
do
for file in "${dir}"/*.txt
do
echo "${file}"
./myprogram "${file}"
done
done
Note the quotes around all variable references. As mentioned in comments, this is an important measure to take. Also keep in mind that quotes are " ", not “ ”.
I have a script to match a pattern, and if it matches, I used the match to append to a variable.
My script works on Bash v3.2.57 and fails on v4.3.30.
Could someone tell me what is wrong with the second ifcondition that matches a pattern here?
#!/bin/sh
if [ -f .file-to-read ]; then
while read p; do
echo "yes"
if [[ $p =~ "#user/"(.+)"#"[0-9]+"."[0-9]+"."[0-9]+ ]]
then
var="$var<#user/${BASH_REMATCH[1]}|$p>\\n"
fi
done < .file-to-read
fi
The error message is
Syntax error: "(" unexpected (expecting "then")
I figured out the issue. The problem was the file was being executed in #!/bin/sh when it should have been #!/bin/bash
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 ]]