Unix case statement in OSX - bash

I am trying to port some Windows bat files to Mac shell scripts
I get a syntax error when executing a file containing the following case statement:
case ${1} in
( C | c ) echo "hoera";;
where argument ${1} is given as 'C'
The message is:
-bash: even: line 6: syntax error near unexpected token `newline'
'bash: even: line 6: ` ( C | c ) echo "hoera";;
I think the syntax is correct according to the documentation. Where am I going wrong?

Syntax should be:
case $var in
CASE1) COMMAND-LIST;;
CASE2) COMMAND-LIST;;
esac
Each case consists of one or more shell patterns, separated by pipes - you don't need an opening parenthesis to match the closing one. Your example should instead be:
case ${1} in
C|c) echo "hoera";;
Or possibly:
case ${1} in
[Cc]) echo "hoera";;

Related

Shell sed - "unterminated `s' command" in Windows / "unescaped newline inside substitute pattern" in MAC

I am trying to fix an old shell script that used to run on MAC to be runnable on Windows... But I am facing errors in both machines WRT one particular sed operation. The code snippet is :
echo "1 :: ${1}"
echo "2 :: ${2}"
sed -i.bak 's/^export \('${1}'=\)\(.*\)/export \1"'"$(sedscape ${2})"'"/' ~/.bash_profile
rm -f ~/.bash_profile.bak
The function sedscape is :
function sedscape() {
echo " >>> sedscape"
echo "1 :: ${1}"
echo ${1//\//\\/} | sed 's/'\''/'\'\\\\\'\''/'
}
This gives me following error on Windows:
1 :: AWS_ACCESS_KEY
2 ::
sed: -e expression #1, char 59: unterminated `s' command
and on MAC:
1 :: AWS_ACCESS_KEY
2 ::
sed: 1: "s/^export (AWS_ACCESS_ ...": unescaped newline inside substitute pattern
.bash_profile has just one line :
export AWS_ACCESS_KEY="aaaa"
As far as I can understand, it is readin every line from .bash_profile starting with "export" and replacing the value of it with whatever is in "2" ... but it fails even before the "sedscape" half (second half) of the expression is evaluated.
Correct me if the understanding of the sed command is wrong.
PLEASE HELP !!!
The echos in sedscape were creating the errors... simply removing them sloved the issue.
echo " >>> sedscape"
echo "1 :: ${1}"

Combining multiline variables as columns in bash

I have to multi-line string variables each with an equal number of lines. I'm trying to make each a column of a new variable called seqinfo, but I am getting a syntax error. Any idea what the problem is?
Code:
seqinfo=$(paste <(echo "$var1") <(echo "$var2"))
Error message:
command substitution: line 13: syntax error near unexpected token `('
command substitution: line 13: `paste <(echo "$var1") <(echo "$var2"))'
You can simply do it like this :
var1='
some
fruits
now'
var2='
even
more
lines'
echo "${var1}${var2}"
Output
some
fruits
now
even
more
lines
Regards!

logical or | Unix

I'm trying to realize a simple shell program that display french phone numbers contained in a file.
Here is my basic shell
#!/bin/bash
#search of phone numbers
t=( \+ | 00 )33[1-9][0-9]{8}
t2=( \+ | 00 )33[1-9][0-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}
t3=( \+ | 00 )33[1-9][0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}
grep -e $1 ( $t | $t2 | $t3 )
Here is my input file :
phone_number.txt
+33143730862
00335.45.45.45.45
+332-45-45-45-45
+334545454554454545
I keep getting this errors :
./script_exo2.sh: line 5: syntax error near unexpected token `|'
./script_exo2.sh: line 5: `t=( \+ | 00 )33[1-9][0-9]{8}'
./script_exo2.sh: line 6: syntax error near unexpected token `|'
./script_exo2.sh: line 6: `t2=( \+ | 00 )33[1-9][0-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}'
./script_exo2.sh: line 7: syntax error near unexpected token `|'
./script_exo2.sh: line 7: `t3=( \+ | 00 )33[1-9][0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}'
./script_exo2.sh: line 9: syntax error near unexpected token `('
./script_exo2.sh: line 9: `grep -e $1 ( $t | $t2 | $t3 )'
Your t2 and t3 have one more digit than the samples you're trying to match. Also, you need to quote the arguments, and get rid of those spaces:
#!/bin/sh
t='(\+|00)33[1-9][0-9]{8}'
t2='(\+|00)33[1-9]-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}'
t3='(\+|00)33[1-9]\.[0-9]{2}\.[0-9]{2}\.[0-9]{2}\.[0-9]{2}'
exec grep -E -e "$t|$t2|$t3" "$#"
I've used sh instead of bash, as we're not using any Bash features not available in standard POSIX shells (e.g. dash).
I've used single quotes above for the definitions of t, t1 and t2, and double quotes where they are to be substituted.
I've told grep to understand Extended Regular Expressions via the -E flag, and I've put the pattern as the argument to the -e ("expression") flag to grep.
The grep process execs in place of the shell, as there is no reason to fork for it.
I've passed the full set of input arguments "$#" so you can give extra options to grep (such as -w, -n or -o, for example), and choose whether to supply a file or stream stdin to your script.
Note also that if you're willing to accept a mix of ., - or nothing separating digit pairs, you can simplify your three expressions to just one:
(\+|00)33[1-9][0-9]([-.]?[0-9]{2}){4}
and the script becomes
#!/bin/bash
exec grep -E -e '(\+|00)33[1-9]([-.]?[0-9]{2}){4}' "$#"
If you need the delimiters to match, then you can use a capture group for that:
#!/bin/bash
exec grep -E -e '(\+|00)33[1-9]([-.]?)[0-9]{2}(\2[0-9]{2}){3}' "$#"

shell : syntax error near unexpected token `fi'

Getting syntax error near unexpected token `fi' while executing below code.
below is the output:-
: command not founde 2:
: command not founde 7:
run_billing.sh: line 22: syntax error near unexpected token `fi'
'un_billing.sh: line 22: `fi
The script is:
#!/bin/sh
#
# main
#
NARGS=$#
if [ $NARGS -lt 2 ]
then
echo "$PRG_NAME: error: incorrect number of arguments ($NARGS)";
echo "Usage: $PRG_NAME [Time]";
echo "Time format - pin_virtual_time format. e.g:- 062000002015.00";
echo "Example: sh run_billing.sh 062000002015.00";
exit 1
fi
if [ $NARGS -eq 2 ]
then
echo "Run Billing script - pin_bill_day";
pin_virtual_time -m2 $1;
pin_bill_day;
fi
Your line endings are screwed up, each line is terminated by a CR/LF rather than just an LF.
If you do something like od -c run_billing.sh, you'll see them there as \r characters as per my test script (the ^M characters are CR):
if [[ 1 -eq 1 ]]^M
then^M
echo x is 1^M
fi^M
^M
0000000 i f [ [ 1 - e q 1 ] ]
0000020 \r \n t h e n \r \n \t e c h o x
0000040 i s 1 \r \n f i \r \n \r \n
0000054
And, when that file is run, we see a similar issue.
That's why you're getting the weird error output, because the CR is moving the cursor to the start of the line before continuing the message, overwriting some of what it's already output.
For example, lines 2 and 7 of your script (the supposedly blank lines) contain a single CR character which is interpreted as a command which doesn't exist. So, for line 2, you see (superimposed):
run_billing.sh: line 2:<CR>
: command not found
=========================== giving:
: command not founde 2:
exactly what you see.
You need to modify the file to remove those CR characters, a couple of ways of doing that are given in this excellent answer.

Bash Script Unexpected End of File - new coder

I'm new to bash, I'm just trying to pick in up in my free time. I'm writing a simple script that prints out the filename I supply as many times as there are characters in the filename, and then positive or negative based on the second argument. I'm getting an unexpected end of file error on this script, and I'm not sure why.
#!/bin/bash
FILENAME=$1
NUMBER=$2
COUNT=0;
STR=${#FILENAME}
while ($COUNT < $STR){
echo $FILENAME
$COUNT++
}
if ($NUMBER < 0)
echo "Negative"
if ($NUMBER > 0)
echo "Positive"
exit
I'm executing with
./script1.sh hello 2
and I'm expecting the output to be
hello
hello
hello
hello
hello
Positive
If anyone could shed some light as to what's going on with the error, that'd be great.
edit: forgot to add the second condition to add to the COUNT variable, and now I'm getting an error:
line 9: syntax error near unexpected token `{'
line 9: `while ($COUNT < $STR){'
Basically you are confusing Bash's syntax with some other language.
In Bash, if's syntax (if not using else) is if condition;then commands;fi. Your code does not have then and fi. Also, the condition should be (( $NUMBER > 0 )).
The syntax of while is while condition;do commands;done, and your code is also wrong.

Resources