syntax error: "(" unexpected when sh a file [duplicate] - linux

This question already has answers here:
Why does process substitution not work in a shell script?
(1 answer)
Bash script process substitution Syntax error: "(" unexpected
(3 answers)
Closed 5 years ago.
i running a file containning such a command
comm -3 <(cut -f 1 -d' ' <./atoz |sort) <(cut -f 1 -d' '
It succeeds when it was run outside of the file.
But i get error:"file1: 1: file1:Syntax error: "(" unexpected ", when i type
sh file1 file2 and try to run the command from the file1.
The file have no any #!/bin/bash or .sh suffix since i dont have the background to solve this kind of problem..
Does somebody know how to solve it? Thanks a lot

Process substitution (<()) is a bash feature. It is not supported with /bin/sh, which guarantees only features present in the POSIX sh specification (on platforms conformant with 1992-era or newer POSIX specifications; on old ones, it could be 1970s-era Bourne).
Use bash yourscript, or a #!/bin/bash shebang, to run this file.

Related

command substitution: line 72: syntax error near unexpected token `(' [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 1 year ago.
I'm still figuring out some niches with bash scripting, I get the following error in my bash script:
updated_names=$(comm -23 <(echo ${current_run_names} | tr " " "\n" | sort) <(echo ${started_run_names} | tr " " "\n" | sort) )
updated_names =$(echo ${updated_names} | cut -d' ' -f1)
Not sure what I've been doing wrong, have deleted various tokens but still continue to get the same error.
How I run it
sh conversion.sh -r directory_location
Supposing that the first of these lines ...
updated_run_names=$(comm -23 <(echo ${current_run_names} | tr " " "\n" | sort) <(echo ${started_run_names} | tr " " "\n" | sort) )
update_run_names=$(echo ${new_run_names} | cut -d' ' -f1)
... is the line 72 referenced in the error message, the issue is most likely with your process substitutions. These are the two fragments of the form <( command ).
Process substitution is a Bash extension to the POSIX shell language. It is not recognized by most other shells, and it is not recognized by Bash itself when it runs in POSIX mode. There is more than one way to get Bash running in POSIX mode, but one of them is to invoke it via the name sh, which is what you are doing.
Note well that the shebang line at the top of your script has a functional role only when you launch the script directly, by making it executable and launching it by name:
./conversion.sh -r directory_location
When you instead launch it as shown in the question, by naming it as an argument to sh, the shebang line is just a comment, and Bash runs (as sh) in POSIX mode.
Note also that although it is conventional for some other language interpreters (Python, especially), it is not conventional to use env in shell script shebang lines.

Move files with exception in bash [duplicate]

This question already has answers here:
Move all files except one
(14 answers)
Closed 3 years ago.
It works fine when I wrote this code on bash shell
mv -v `pwd`/!(.git) `pwd`/NewDir
But if I create shell script file like below,(name of this file is "s.sh")
#!/bin/bash
mv -v `pwd`/!(.git) `pwd`/NewDir
it returns error
./s.sh: line 2: syntax error near unexpected token `('
./s.sh: line 2: `mv -v `pwd`/(!.git) `pwd`/NewDir'
How can I fix it?
!(.git) is an extended glob, you need to enable extglob to make it work in a non-interactive shell. And I think instead of calling pwd twice, you can use PWD variable in this case.
shopt -s extglob
mv -v "$PWD"/!(.git) "$PWD/NewDir"

How to echo this entire code to .bashrc without leaving out characters/strings? [duplicate]

This question already has answers here:
How do I properly quote this bash pipeline for watch?
(2 answers)
Closed 3 years ago.
How can I echo this entire piece of code to .bashrc without leaving out a single character?
# automatic logging of terminal input/output
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/$(date +"%Y-%m- %d_%H:%M:%S")_terminal.log)
When I attempt to enter the following into terminal:
echo "the above code" >> ~/.bashrc
I get the following appended to .bashrc which is nothing like "the above code", its short about 45 or so characters.
# automatic logging of terminal input/output
test script == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/2019-05- 08_09:09:19_terminal.log)
As you can see, it's leaving out A LOT of the original code. I understand this has a lot to do with the number of different quotations and placement, but without altering my code much, or at least to the point where it can still function as its intended, how can I go about getting this to echo to the file properly?
Thank you for every nanosecond of your time.
Wrap your echo'd string with single quotes ' instead of double "

Capturing command output in a shell variable isn't working [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I am new to shell scripting, and need to output a series of commands to a local variable in a shell script, but keep on failing. For instance, the output of grep -c to a variable that will be use in an if statement. If anyone can redirect me over to a source that explains the process, I will appreciate.
#!/bash/sh
myVar = ls ~/bin | grep -c $0
Posting your code at shellcheck.net gives you valuable pointers quickly:
myVar = ls ~/bin | grep -c $0
^-- SC2037: To assign the output of a command, use var=$(cmd) .
^-- SC1068: Don't put spaces around the = in assignments.
^-- SC2086: Double quote to prevent globbing and word splitting.
If we implement these pointers:
myVar=$(ls ~/bin | grep -c "$0")
Also note that your shebang line has an incorrect path - the #! must be followed by the full path to the executing shell's binary.
Resources for learning bash, the most widely used POSIX-compatible shell:
Introduction: http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html
Guide: http://mywiki.wooledge.org/BashGuide
Cheat sheet: http://mywiki.wooledge.org/BashSheet

basic Linux bash command clarifications: awk and others [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am using Mac OS and Bash terminal to do some basic scripting.
I have some questions which I could not find answers when I searched online
(or may be my search keywords were bad)
Firstly, I see there are three paths: /bin, /usr/bin and /usr/local/bin. some commands like grep are found in /usr/bin, while some others like ls are found in /bin.
why we have three different bins and what criteria goes to put commands like these over there
Secondly, I want to know the difference between using ' and `.
echo `date`
Fri Jan 10 10:36:52 PST 2014
awk '{print $1}' test.txt
1
2
3
4
if I try:
awk `{print $1}` test.txt
-bash: {print: command not found
awk: syntax error at source line 1
context is
>>> test. <<< txt
awk: bailing out at source line 1
so when to use ' and `.
Lastly, the above awk with print works. but this does not work
awk '{echo $1}' test.txt
can print and echo not be used interchangebly?
P.S: I am a beginner with bash scripting, please be kind
Brief answers not necessarily exhaustive:
/bin is usually for system commands
/usr/bin is for commands for users
/usr/local/bin is for software not typically installed by a distribution or release of an OS.
Quotes:
Things in single quotes are not touched by the shell
Things in double quotes are variable expanded by the shell ($var is expanded to what $var contains)
Things in back quotes are executed as a command and the output of that command replaces what was in the back quotes. You can also use $(echo Hello) to achieve the same thing.
You cannot easily mix awk and shell inside a string passed as a script to awk. (ex: awk '{echo $1}' test.txt)
awk `{print $1}` test.txt
This fail since you are using back tics and not single quotes.
Correct:
awk '{print $1}' test.txt
It will then print first field of all line in the file.
awk '{echo $1}' test.txt
Does not work since echo is not an awk command.
Tell us what text you have and what you like to get out of it..
You need to read a book. I recommend Shell Scripting Recipes: A Problem-Solution Approach (http://www.amazon.com/Shell-Scripting-Recipes-Problem-Solution-Approach/dp/1590594711). Read it, work through the exercises, and then come back with questions if you have any (or even better ask them at the comp.unix.shell newsgroup where that book's author and all the other shell experts hang out).
print is an awk command.
echo is a shell command.
backticks and $() run shell commands.
' quotes the contents such that the shell does not expand anything inside them.
$(echo hello) is the same as: echo hello. By doing this you ask the bash interpreter to execute the result of that command which is "hello". But hello is not a command (unless if you define it yourself).
These examples are all correct:
foo=$(echo hello)
echo ${foo}
foo=(echo "echo hello")
${foo}

Resources