Error on certain line: Syntax error: "(" unexpected - bash

Have an error "Syntax error: "(" unexpected" when execute an script:
sync.sh: 11: sync.sh: Syntax error: "(" unexpected
line 11 contains on this:
declare -a FOLDERS=('/scripts' '/backup')
and on the top of script have the interpreter:
#!/bin/bash
Execute the script with:
sh /wdmycloudex2/$(hostname)/scripts/sync.sh
/wdmycloudex2/RASPBIAN/scripts/sync.sh: 11: /wdmycloudex2/RASPBIAN/scripts/sync.sh: Syntax error: "(" unexpected
The firsts 11 lines:
#!/bin/bash
IP='10.0.1.7'
PORT='443'
HOSTNAME=$(hostname)
DATE=$(date +%d%m%Y_%H%M%S)
SOURCE='/scripts'
DEST='/wdmycloudex2'
declare -a FOLDERS=('/scripts' '/backup')
anybody know and explain what's the problem?

The header #!/bin/bash is ignored when you start the script with sh sync.sh.
It will go better with bash /wdmycloudex2/RASPBIAN/scripts/sync.sh or
chmod +x /wdmycloudex2/RASPBIAN/scripts/sync.sh
/wdmycloudex2/RASPBIAN/scripts/sync.sh

Related

Why does bash prohibit a shell function named `df`?

I am using GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu).
I have the following lines in one of my startup files:
df() {
printf "Hello, world!\n"
}
When source that file, I get this error:
-bash: sh/interactive.sh: line 109: syntax error near unexpected token `('
-bash: sh/interactive.sh: line 109: `df() {'
However, if I change the function name from df to dir or ef or anything_else I don't get the error.
I'm assuming that df is somehow a reserved word, but when I checked this list of reserved words in bash I couldn't find it. (And I don't think it deserves to be one, anyway!)
So, can anyone shed some light on this? Why does bash prohibit me from defining a shell function named df?
This happens because you've previously defined an alias for this name. Aliases are simple string prefix substitutions, and therefore interfere with function definitions:
$ alias foo='foo --bar'
$ foo() { echo "Hello"; }
bash: syntax error near unexpected token `('
This is equivalent to (and fails with the same error as)
$ foo --bar() { echo "Hello"; }
bash: syntax error near unexpected token `('
To declare a function with a name that's been overridden with an alias, you can use the function keyword:
$ alias foo='foo --bar'
$ function foo() { echo "Hello, $1"; }
$ foo
Hello, --bar

Unexpected end of file error with every bash script I write [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
I must be missing something here because no matter what bash compiler I use online and no matter what bash script I write I get the same Unexpected end of file error.
Here's my script
function hello { echo 'hello' } hello
I've also tried this
#!/bin/bash
hello_world () {
echo 'hello, world'
}
jdoodle.sh: line 2: $'\r': command not found
jdoodle.sh: line 3: syntax error near unexpected token `$'{\r''
jdoodle.sh: line 3: `hello_world () {
'
And this
#!/bin/bash
f() { $branchName = "branch" echo $branchName}; f
hello_world
I'm using this tool:
https://www.jdoodle.com/test-bash-shell-script-online/
Why can't I write a simple bash script?
you are missing a ; after your echo command. i just ran this on git bash, recieved the same error then looked at some documentation. After adding the semicolon it functioned perfectly.
function hello { echo 'Hello World!'; }
hello

unix shell script not working gives me error

what is wrong with my code this is it -
export -P ttiUsername="Username: " || ^
i get an error about command ^ and the -p
here is my error
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: export: -P: invalid option
export: usage: export [-nf] [name[=value] ...] or export -p
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: ^: command not found
edit: i lowercased the p but it doesnt prompt u to enter username like it should
The problem with your code is this line:
export -P ttiUsername="Username: " || ^
It's wrong because export doesn't read data, -P is not a valid option, and ^ is not a valid command. None of it makes sense if the goal is to read data.
To read data from the user, use read:
read -p "Username: " ttiUsername
echo "You wrote: $ttiUsername"

Delimiter Warning in shell Script with psql

I get a Delimter Error in a Shell Script:
#!/bin/sh
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
;`
EOF
echo $result
#EOF
The result of the Script is fine. But I get two warnings:
./filename.sh: line 13: warning: here-document at line 8 delimited by end-of-file (wanted `EOF')
./filename.sh: line 9: EOF: command not found
What is the problems here? Thank you!
You have the start of your here-doc inside of your command, but the EOF is outside of your command.
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
EOF
`
The ; seems wrong here too (at least it threw an error for me).

Bash script error. What is wrong with this script?

I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)

Resources