Fixing bash: syntax error near unexpected token `>' - bash

I have a bash syntax error problem. I am writing a script to insert information in files without opening the file like so
for i in 1 2 3 do echo This is a sample text > sample-$i.txt done
But it gives the following error
bash: syntax error near unexpected token `>'
Why is that? And how can I fix this?

You are missing two semicola. This is the fixed version:
for i in 1 2 3; do echo This is a sample text > sample-$i.txt; done

Appending data to the file doesn't require opening the file. One of the greatest things with Linux scripting is the way that data can be piped around and into/out of files.
> will empty the file but >> will append to the file

Related

How do I solve the issue of "Error (genfile::MalformedInputError)" when attempting to use QCTools_V2

New user to stackoverflow.
I am trying to read in a .gen.gz file into qctools and retrieve specific SNPs from a .txt file which I've already specified.
Originally the .gen.gz file was in .bgen format but I deleted the .bgen suffix replacing it with .gen.gz (not sure if this is correct?). I am receiving an error:
"Error (genfile::MalformedInputError" source is malformed on line 1"
I read elsewhere that this could be an issue with trailing whitespace in the file, and I was wondering if anyone could provide guidance as to how to remove this trailing whitespace.
I have tried using:
sed -i 's/ /' filename
But my terminal gives me this error:
"sed: 1: "ukbxxxx_c1_b0_v3.bgen": invalid command code u"
Any help would be GREATLY appreciated
-Noah

error after installing magento

Parse error: syntax error, unexpected 'Sns_Avaz_Model_Observer'
(T_STRING) in
/home/ganyobiclothing/public_html/app/code/local/Sns/Avaz/Model/Observer.php
on line 1
If this is the sns-avaz-responsive-magento-theme you should check the following page: http://themeforest.net/item/sns-avaz-responsive-magento-theme/11951628/comments
Hi,
This issue only happen with some hosting/server – it compress content file
We’ve fixed this issue on v1.0.1. Please try go to file: >/app/code/local/Sns/Avaz/Model/Observer.php open it and add space after re-save.
Ex: "<?php" ==> "<?php "
P/S: See my screenshot: http://take.ms/v8G03 and If your issue still … please
Thanks!
Source: http://themeforest.net/item/sns-avaz-responsive-magento-theme/11951628/comments
Good luck!
Parse error: syntax error, unexpected 'Sns_Avaz_Model_Observer' (T_STRING) in /home/ganyobiclothing/public_html/app/code/local/Sns/Avaz/Model/Observer.php on line 1
As its saying on line 1 so probably there is some white space or special character thats resulting in this.

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 `else' formatting issue?

I'm pretty new to bash and am having trouble with what I assume is formatting. I'm trying to edit the /etc/profile so it will display a login message for root and a different login message for anyone else. But I'm getting the error bash: syntax error near unexpected token else. I've tried all the different combinations of no semicolon, then on the next line etc but always get the same error. I've tried the lines separately and they display fine (except $HOSTNAME, can't get that to work). When run like this and login with root it will just jump to "Welcome $USER...".
Anyone suggestions would be appreciated!
if [ "$UID" -ne 0 ]; then
echo -e "\033[40;37;7m Danger! Root is doing stuff in `pwd`\033[0m"
else
echo "Welcome $USER! You are working on `$HOSTNAME` in `pwd`."
fi
As written, above works for me - but, as pointed out, you should change your test to -eq 0.
For the syntax error near unexpected token problem - I will guess that your file contains embedded 'control codes', i.e. most likely a carriage return \r.
Try:
cat -e ~/your_profile
see any non-printable characters? if so, remove them (cat options may vary - check you manpage) or
od -c ~/your_profile

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