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"
Related
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.
I was making this really simple arithmetic script but got this odd error when I try to pass an * operator.
Syntax: [command] [num1] [num2] [arithmetic operator]
Here is the code:
result=$(($1$3$2))
echo "Calculation result is: $result "
Passing a '*' operator like this:
bash my_script.sh 1 2 *
... returns the following error:
line 7: 3Access: value too great for base (error token is "3Access")
I botched together a fix for it by replacing * with \\\* through a test statement. Though I would like to understand why this error occurs.
The only threads I found refer to the error regarding above-octal values being assumed by BASH to be octal. But it's unclear to me why * is being looked at as a numerical value at all.
The * has a special meaning in the shell language. It is used by filename expansion and expands to all non hidden files and folders in the current directory and passes them as individual arguments to the script.
Just try this script to see:
#!/bin/bash
# star.sh
echo "$1"
echo "$2"
echo "$3"
# and so on ...
Now run it:
bash star.sh *
It will print the first 3 files in your current directory.
To avoid filename expansion to happen you need to quote the *. Like this:
bash your_script.sh 1 2 '*'
or escape it
bash your_script.sh 1 2 \*
See Bash Manual: Filename Expansion.
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'm putting together a simple backup script that will tar contents of a folder, then move that file to a backup server. The script makes sure that the tar file exists and is not zero bytes before moving around.
The problem is that the script is dying on one of the IF lines
if [ -f /www/archives/pdf/pdf_201207021048.tar && 11294720 -gt 0 ]; then
echo "tar file exists and is greater than 0 bytes."
else
echo "tar file does not exist or is zero bytes"
fi
The error in the console is:
./backup_pdf.sh: line 49: [: missing `]'
Line 49 is the if statement above.
The script is successfully verified with
bash -n backup.sh
What's wrong that bash is seeing a missing ']', yet it passes the syntax check?
The && operator separates commands, so your [ and your ] aren't part of the same command, as is required. Either use two sets of brackets with a && between them, or use the -a operator. Most people prefer the first option these days.