How to use cat command with ignore conditions in bash script? - linux

The following command works fine on the command line, but not in a bash script.
cat dir/!(00|01)/* > all.txt
When executing the same command in a bash script I get the following error:
../scripts/preprocess.sh: line 8: syntax error near unexpected token `('
../scripts/preprocess.sh: line 8: ` cat dir/!(00|01)/* > all.txt'
Does anyone know how to get this to work in a script?
Thanks

You need to set the same shell option in the script that is set at the command line, namely extglob.

Related

Is there a package a need to install to use operators with Bash? [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

List command not displaying all output

Trying to connect to multiple machines using bash on a machine that has the public ssh keys for the others and run a command on them to display the output on this machine. If I use the '$a' variable as in the code below when I execute the script I get this uncompleted output
bash: total: command not found
bash: line 1: drwxr-xr-x: command not found
bash: line 2: drwxr-xr-x: command not found
bash: line 3: -rw-r--r--: command not found
bash: line 5: -rwxr-xr-x: command not found
....
However if I use the commented block without calling the $a variable the bash prints the correct output of the command.
The code executed:
#!/bin/bash
a=$(ls -lah)
for i in "machine1" "machine2"
do ssh root\#"$i" "$a; exit;"
*#do ssh root\#"$i" "ls -lah; exit;" - displays accordingly*
done
Your code does not work because a=$(ls -lah) assigns ls -lah's output to variable a. Change it to a='ls -lah' and it will be ok.
See command substitution section in your bash's manual
Hi I have another answer which i have been using for a long time.
declare machines=("user#machine1" "user#machine2")
command="ls -lah"
for machine in "${machines[#]}"
do
ssh "$machine" "$command"
done
$a did not work because it will be executed.

syntax error when compare two files using shell script [duplicate]

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Open multiple tabs and execute command in shell script

#!/bin/bash
tab="--tab"
cmd="bash -c 'python';bash"
foo=""
for i in 1 2 3; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[#]}"
exit 0
i'm using this scirpt to open multiple tabs using shell script.
call it multitab.sh and execute this way user#user:~$ sh multitab.sh
currently this script supposed to open 3 tabs and all of them will execute python command.
but when i execute it, throws en error
multitab.sh: 8: multitab.sh: Syntax error: word unexpected (expecting ")")
What is the reason of this error? How can I make this script to execute 3 different commands?
I've already gone through. below SOF threads but none of them worked for me.
https://askubuntu.com/questions/315408/open-terminal-with-multiple-tabs-and-execute-application
https://askubuntu.com/questions/500357/opening-multiple-terminal-tabs-and-running-command
https://askubuntu.com/questions/521084/bash-script-for-multiple-tabs-program-running
This is because you are running the script with sh, where the += syntax to add elements is not available:
foo+=($tab -e "$cmd")
# ^^
So all you need to do is to run the script with Bash:
bash multitab.sh
Or just using ./multitab.sh (after giving executing mode to the file), since the shebang in the script (#!/bin/bash) already mentions Bash.
From the Bash Reference Manual:
Appendix B Major Differences From The Bourne Shell
- Bash supports the ‘+=’ assignment operator, which appends to the value of the variable named on the left hand side.

Bash script process substitution Syntax error: "(" unexpected

I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Resources