Call a function from an external script - bash

I have 2 scripts:
/home/bin/test.sh
#!/bin/bash
. /home/bin/test_functions.sh
test
/home/bin/test_functions.sh
#!/bin/sh
test()
{
echo "this is a test"
}
I wanted to call the function from the external script and execute it in the main script. Yet I've been receiving these errors:
'home/bin/test_functions.sh: line 2: syntax error near unexpected token `
'home/bin/test_functions.sh: line 2: `test()
What could be wrong with what I'm doing?

It appears that test_functions.sh is in DOS format and bash is choking on the \r\n line endings. Use dos2unix to convert it to UNIX line endings.
You can tell because what bash is trying to output is this:
/home/bin/test_functions.sh: line 2: syntax error near unexpected token `\r'
/home/bin/test_functions.sh: line 2: `test()\r'
But the carriage returns \r cause the single quotes to end up at the beginning of the error messages, overwriting the leading /.

Related

If bash variable value contains space, the script chokes [duplicate]

This question already has answers here:
Reading complete line in 'for' loop with spaces, tabs with multiple input files
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 4 years ago.
I have a bash script that parses an xml file (name: myFile.xml) that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<params>
<username>jDoe</username>
<password>123abc</password>
<fullname>John Doe</fullname>
<email>johndoe#example.com</email>
<phone>1234567890</phone>
<country>Italy</country>
</params>
In that bash script, I parse each value of the xml file into a variable for further use. So far the bash script looks like that:
for i in $(xmlstarlet select --template --match "//params/*" --value-of "concat(name(),'=\"',text(),'\"')" -n myFile.xml)
do
eval $i
done
#debugging:
echo $username
echo $fullname
echo $password
When I run this script
./myScript.sh
I get the following output:
./myScript.sh: eval: line 34: unexpected EOF while looking for matching `"'
./myScript.sh: eval: line 35: syntax error: unexpected end of file
./myScript.sh: eval: line 34: unexpected EOF while looking for matching `"'
./myScript.sh: eval: line 35: syntax error: unexpected end of file
jDoe
123abc
Apparently, because the tag <fullname> has a value of 2 words separated by space the script chokes. If I replace it's value "John Doe" with another (with no space) like: "JohnDoe" or "John_Doe" the script works fine!
Any suggestions as to how I can pass to a bash variable a value that contains space?
Of course, I would like to maintain the loop because
1. the actual script has too many parameters
and 2. the parameters are not always the same (they vary from one xml file to another)...
(and to cut the long story short, this is what I would like to achieve: fullname="John Doe")

ksh error "syntax error at line 5: `(' unexpected"

I have a php file that print a number, and I want to use this number in my ksh file so I do this but it is not working.
#!/bin/sh
testt = $(php /path/to/count.php)
echo $testt
I want that the testt variable is assigned the number value.
The shell's tokenizer is white-space sensitive. Use
testt=$(php /path/to/count.php)
Note: no blanks around =.

Implementing which function in shell [duplicate]

This question already has answers here:
Bash script syntax error "do"?
(3 answers)
Closed 7 years ago.
I'm trying to implement Unix's which function, but keep getting syntax errors, on what I (think) is legal? This is my implementation:
IFS=":"
x=false
for i in $*
do
for j in $PATH
do
if [ -x "${j}/$i" ];then
echo $j/$i
x=true
break
fi
done
if [ $x == false ]; then
echo my_which $i not found in --$PATH--
fi
x=false
done
I keep getting the following error
$ bash which.sh
: command not found:
'which.sh: line 5: syntax error near unexpected token `do
'which.sh: line 5: `do
Your script has DOS newlines. Use dos2unix to convert it, or open it in an editor that can do the conversion for you (in vim, you would run :set fileformat=unix and then save with :w).
$ bash which.sh
: command not found:
'which.sh: line 5: syntax error near unexpected token `do
'which.sh: line 5: `do
See the 's at the beginning of those lines? Those are supposed to be at the end of the line.
What's happening, however, is that your dos have a hidden $'\r' character after them, which sends the cursor back to the beginning of the line. Thus, instead of seeing do as a valid token, or correctly printing
# this is the error you would get if your "do" were really a "do", but it were still
# ...somehow bad syntax.
syntax error near unexpected token `do'
...we get...
# this is the error you get when your "do" is really a $'do\r'
'yntax error near unexpected token `do
...because a carriage return is sitting between the do and the '.

parameter expansion syntax bash

I am trying to write a short script that will take two command line parameters as file extensions and change all files with the first extension to have the second extension. I am pretty sure the following script should work but for some reason it gives me a syntax error on the line where the variable name is defined and I am not sure why. I am rather new to bash scripting so any help would be greatly appreciated!
for f in "*$1" do
name=${f%.*}
mv $f "$name$2"
done
The error message printed by Bash looks like:
./script: line 4: syntax error near unexpected token `name=${f%.*}'
./script: line 4: `name=${f%.*}'
The reason is that you are missing a ; or newline before do. Also you don't want to quote * in "*$1", since the * will be taken as a literal. Corrected script:
#!/usr/bin/env bash
for f in *"$1"; do
name=${f%.*}
mv "$f" "$name$2"
done

syntax error near unexpected token `

The following script is intended to run the program "senna" on all files in a directory and write the output for each file (preserving the input file name) into another directory
for file in ./Data/in/*;
do
./senna -iobtags -usrtokens -posvbs -srl < $file > ./Data/out/$file
done
On trying to execute the script, the following error arises.
-bash-4.0$ sh run.s
'un.s: line 1: syntax error near unexpected token `
'un.s: line 1: `for file in ./Data/in/*;
The script has the lines of code exactly as above and there is no `. Perhaps it implies something else. Help with error resolution would be appreciated.
The line endings in the script are wrong. Pass it through dos2unix to eliminate the CRs.

Resources