Implementing which function in shell [duplicate] - bash

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 '.

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")

how to hide warning in bash script [duplicate]

This question already has answers here:
Suppress warning output in bash
(3 answers)
here-document gives 'unexpected end of file' error
(10 answers)
Closed 6 years ago.
I have a bash script I've been using for ages. It has a few lines it just start/stop some jobs in database. It is very simple and it never fails. Last year, it facts in December so a few days ago :) I migrated whole data to a new machine. And then my problem starts.
Now script works as it worked before but now it returns warning. Below output:
[oracle#SVMSLUATTIADB1 ~]$ ./batch.sh stop
./batch.sh: line 87: warning: here-document at line 85 delimited by end-of-file (wanted `!')
./batch.sh: line 87: warning: here-document at line 85 delimited by end-of-file (wanted `!')
Batch_Executer_1 [NOT OK]
Batch_Executer_2 [NOT OK]
[oracle#SVMSLUATTIADB1 ~]$
The piece of code mentioned in warning is:
batch_stop()
{
for BATCH in $BATCHES
do
SQLRES=`sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
` << line 87
done
}
So the question is how to avoid/hide this warnings. It is not critical, script works as it worked but the view of warning disturbs. I see that before I had bash version 3.2.25 but now it is bash 4.1.2.
I tried to modified my code and redirected output :
{SQLRES=`sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
` } 2>/dev/null
but it didn't help :(
The warning is caused by invalid here document syntax. Namely, the second limit word ("!") is missing.
Also, don't use back quotes for command substitution. Use the $(...) syntax instead, as it is more readable, and it can be nested.
The fixed version:
SQLRES=$(sqlplus -S "/ as sysdba" << !
exec dbms_scheduler.disable('"AIT"."${BATCH}"', TRUE);
!
)
Note, the here document ends on a line containing only the limit word (the exclamation mark, in particular). So it is not allowed to indent the second limit word, for example.

Call a function from an external script

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 /.

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

-bash: syntax error near unexpected token `)'

I'm probably missing something very basic. I have a web script that tells a shell script to update some database records. These records are for statistics, and this way the web script does not have to wait while the database records are updated. However, I can't actually make the shell script work running it from commandline. Here is the code that I'm trying:
perl async_sql.pl 'UPDATE some_table set i = i + 1 WHERE (n in (\'328430\',\'334969\',\'330179\',\'335290\',\'335285\',\'335284\',\'335264\',\'335145\',\'335146\',\'335147\',\'335148\',\'335149\',\'335230\',\'335201\',\'335198\',\'335196\',\'335167\',\'335151\',\'335152\',\'335143\',\'334969\',\'334972\',\'334977\',\'334978\',\'334979\',\'334980\',\'334982\',\'334983\',\'334984\',\'334934\',\'334947\',\'334948\',\'334950\',\'334992\',\'335014\',\'335026\',\'335030\',\'335032\',\'334864\',\'334862\',\'334861\',\'334858\',\'334855\',\'334852\',\'334850\',\'334849\',\'334848\',\'334847\',\'334844\',\'334842\'))'
Bash tells me:
-bash: syntax error near unexpected token `)'
What am I missing?
It is not possible to escape single quote in single quotes. Use " " instead
perl async_sql.pl "UPDATE some_table set i = i + 1 WHERE (n in ('328430','334969','330179','335290','335285','335284','335264','335145','335146','335147','335148','335149','335230','335201','335198','335196','335167','335151','335152','335143','334969','334972','334977','334978','334979','334980','334982','334983','334984','334934','334947','334948','334950','334992','335014','335026','335030','335032','334864','334862','334861','334858','334855','334852','334850','334849','334848','334847','334844','334842'))"
Also, there are other ways to deal with this problem:
echo "quote'test"
echo 'quote'"'"'test'
echo 'quote'\''test'
echo $'quote\'test'
All these should print quote'test as a single parameter. Which means that another great solution for your problem is:
perl async_sql.pl $'UPDATE some_table set i = i + 1 WHERE (n in (\'328430\',\'334969\',\'330179\',\'335290\',\'335285\',\'335284\',\'335264\',\'335145\',\'335146\',\'335147\',\'335148\',\'335149\',\'335230\',\'335201\',\'335198\',\'335196\',\'335167\',\'335151\',\'335152\',\'335143\',\'334969\',\'334972\',\'334977\',\'334978\',\'334979\',\'334980\',\'334982\',\'334983\',\'334984\',\'334934\',\'334947\',\'334948\',\'334950\',\'334992\',\'335014\',\'335026\',\'335030\',\'335032\',\'334864\',\'334862\',\'334861\',\'334858\',\'334855\',\'334852\',\'334850\',\'334849\',\'334848\',\'334847\',\'334844\',\'334842\'))'
I have only placed a dollar sign $ right before the parameter. That's it. A dollar sign before single quotes turns on ANSI-C Quoting
bash: syntax error near unexpected token `('
this type of error can be solve by the turn on extended globbing by git bash command is "shopt -s extglob"

Resources