Bash issue with unexpected token - bash

The script below seems to work when I run it on its own in vscode it says literal carriage return on the second and third line.
When I put it into a larger script I get this issue
./script.sh: line 64: syntax error near unexpected token `newline'
./script.sh: line 64: `ipv6address=$(hostname -I | cut -d " " -f 2)'
Can any one advise please
#!/bin/bash
ipv6address=$(hostname -I | cut -d " " -f 2)
tee -a <<EOF >/dev/null "$HOME"/ipv6
[network]
# replace the ip with yours
routable_ip ="$ipv6address"
EOF

Related

How to redirect grep to a while loop

Hi I have the following bash script code
group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")
and I get the following error:
./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'
I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside
The problem is in this line:
done << (grep -w "token" "$file")
# ^^
You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:
done < <(grep -w "token" "$file")
# ^ ^
Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

Solaris - Comment Specific Line from File and Add New One

I haven't worked much with solaris, but I'm supposed to be writing a script that searches for a line in a file, comments it out, and writes the correct line below it.
for i in `cat solarishosts`
do
#print hostname
echo ${i}
#get the line number of the expression after the /; save its value to linenum
linenum="$(ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "awk '/%sugrp ALL=\(user\) lines: /usr/bin/su -, /usr/bin/su - user/a{ print NR; exit }' /usr/local/etc/sudoers")"
#overwrite the line # linenum (overwriting just a to add a comment)
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "sed -n "${linenum}"p <<< "#%sugrp ALL=\(user\) lines: /usr/bin/su -, /usr/bin/su - user""
#use the linenum var to make a newlinenum var , this one being one line down from where the commented text was written
newlinenum=linenum+1
#write the line in quotes # the newlinenum position
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 ${i} "sed -n "${newlinenum}"p <<< "%sugrp ALL=\(ALL\) ALL""
done
I'm getting weird errors :
awk: syntax error near line 1
awk: bailing out near line 1
bash: -c: line 0: syntax error near unexpected token `newline'
bash: -c: line 0: `sed -n p <<< #%sugrp ALL=(user) PASSWD: /usr/bin/su -, /usr/bin/su - user'
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `sed -n linenum+1p <<< %sugrp ALL=(ALL) ALL'
It looks like there's an error with my awk syntax ... but it isn't on line 1? And I'm not sure what the error is
I don't have a newline character anywhere in my first sed line?
In my code I escaped the "(' it's complaining about
That's pretty messy. You don't need to ssh into the box 3 times. Your quoting is a big problem. And you never actually write the changes back to the file.
Try this: build up the remote command and call ssh once:
line='%sugrp ALL=(user) lines: /usr/bin/su -, /usr/bin/su - user'
newline='%sugrp ALL=(ALL) ALL'
file=/usr/local/etc/sudoers
awkcmd='$0 == line {print "#" $0; print new}'
cmd=$(
printf "awk -v line='%s' -v new='%s' '%s' %s > %s.new && mv %s %s.bak && mv %s.new %s" \
"$line" \
"$newline" \
"$awkcmd" \
"$file" "$file" "$file" "$file" "$file" "$file"
)
while read -r host; do
echo "$host"
# perform the remote command
ssh -o ConnectTimeout=1 -o ConnectionAttempts=1 "$host" sh -c "$cmd"
done < solarishosts
I use single quotes as much as possible to reduce the need for backslashes in the constant strings, and all variables are quoted when used.

Errors in bash script. Syntax error near unexpected token

Do you know what is wrong with my script as I always get the error mesage:
position frac1 frac2
: command not found:
'/s1_met.sh: line 3: syntax error near unexpected token `do
'/s1_met.sh: line 3: `for lineF1 in $(cat $1); do
Code here:
export IFS=$'\n'
echo "position frac1 frac2";
for lineF1 in $(cat $1); do
if [ $(echo $lineF1 | cut -b 1-2) = "##" ]; then
echo "skip line" >&2;
else
startF1=$(echo $lineF1 | cut -f 4);
stopF1=$(echo $lineF1 | cut -f 5);
fracF1=$(echo $lineF1 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
lineF2=$(grep "$startF1" $2);
if [ -z "$lineF2" ]; then
echo "position $startF1 cannot be found" >&2;
else
fracF2=$(echo $lineF2 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
echo "$startF1 $fracF1 $fracF2";
fi;
fi
done;
There's nothing wrong with it, you must not be running it with BASH.
Edited to say you need to check your line endings, your comment below with the ^M means that you have extra characters on the line. See here.
https://stackoverflow.com/tags/bash/info
Try putting the "shebang" line in the script shebang docs
To do this, run which bash which will tell you something like /bin/bash. Your script should then be:
#!/bin/bash
echo "I'm running with bash!"
Try that, your syntax is OK.

for-loop not working: trying to turn a ls into an array

I'm receiving a snytax error when I run the following code:
#!/bin/bash
for i in (`ls *.nexus`);
do
awk 'NR >5' /path/to/nexus_files/$i | tr -d "'" | tr " " "\n" | sed 's/uce/>uce/g' > /path/to/fasta_files/${i}.fasta
done
error:
-bash: syntax error near unexpected token `(
when I remove parentheses:
-bash: syntax error near unexpected token 'awk'
In your simple example, you can do w/o the ls command
for i in *.nexus ; do
awk ...
done

if conditional in bash giving error

I have a small bash script where I want to get the format of a file.
FILENAME=$1
GET_FILE_FORMAT=`file $FILENAME | grep -i data`
if[[ "$GET_FILE_FORMAT" = *data* ]]
echo "Format Data";
fi
However the output that I get is as follows
./try.bash test.data
./try.bash: line 4: if[[ test.data : data = *data* ]]: No such file or directory
Format Data
./try.bash: line 6: syntax error near unexpected token `fi'
./try.bash: line 6: `fi'
There are a couple of problems here:
You don't have any space after if.
The end of the conditional, i.e. if, isn't indicated.
To fix, say:
if [[ "$GET_FILE_FORMAT" = *data* ]]; then
To prevent getting incorrect information when the file name itself contains the string data, say:
GET_FILE_FORMAT=$(file "${FILENAME}" | awk -F: '{print $NF}')

Resources