Bash function failing with [: too many arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash function failing with [: too many arguments
# 8  
Old 10-10-2017
I found the problem. I really, really hope someone can explain it. The top line does not work. The bottom one does. I manually typed the top line in. I copied and pasted the bottom line from the scripts supplied with the book. The quote marks look different. What in the world is going on here? I'm doing this on KDE Neon and in vim.

Code:
for directory in "$ourpath"
for directory in “$ourpath”

# 9  
Old 10-11-2017
Quote:
Originally Posted by jakeroberts
I found the problem. I really, really hope someone can explain it. The top line does not work. The bottom one does. I manually typed the top line in. I copied and pasted the bottom line from the scripts supplied with the book. The quote marks look different. What in the world is going on here? I'm doing this on KDE Neon and in vim.

Code:
for directory in "$ourpath"
for directory in “$ourpath”

I think you have that backwards. The top line should work; the bottom line can't work. The shell command language has rules for constructing commands just like English has rules for constructing sentences. In English, sentences are terminated by a period, a question mark, or an exclamation point. The following is not an English sentence, because a semicolon is not a period, is not a question mark, and is not an exclamation point.
Code:
This is not a valid English sentence;

In the shell command language quoted strings are surrounded by double-quote characters (") or single-quote characters ('). And, the opening-double-quote character () and closing-double-quote character () is not a double-quote character (although in some fonts they look similar). Instead of being characters delimiting a double-quoted string, they are regular characters that are part of an unquoted string (or if there are whitespace characters between those quotes, part of multiple unquoted strings).
# 10  
Old 10-11-2017
The top line has the normal ASCII quotes.
They work in the sense "protect the variable from splitting and globbing".
But in this case a splitting is wanted (as I said in post #7).

The "wrong" (alternate?) quotes in the second line might, after splitting into words, be added at the beginning of the first word and at the end of the last word. So might also spoil it, in this case the first and last path element.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call same function using 2 different arguments

I have a script that uses 2 arguments. I want to call the function part within this script using these same arguments. Below is what I came up with below script so far, any guidance would be helpful. Thank you! cat backup.sh #!/bin/bash function usage { echo "USAGE: $(basename $0)... (6 Replies)
Discussion started by: mbak
6 Replies

2. Shell Programming and Scripting

Bash Regexp failing: unexpected token `('

Hey guys, I'm fairly new to bash scripting, so bear with me :) What I want to achieve is reading a file (.php), find the phrase 'WACHTWOORD' (password) in it, and collect the password and a part of the filename in a list. The filename is always 'settings_{name}.php' and the part I'm looking... (6 Replies)
Discussion started by: Eggie01
6 Replies

3. UNIX for Dummies Questions & Answers

Invoke Function WithOut Non-Positional Arguments

Hello Everyone, Is there a way i can pass the arguments as parameters or variables instead of positional arguments to a function, below i am calling the function defined in a script. Call: notify "Error While Generating The List File: ${GEN_PARAM_LIST9} For Feed Data Validation Errors In... (1 Reply)
Discussion started by: Ariean
1 Replies

4. Shell Programming and Scripting

Need to call a function with arguments

I need to call a function within a code with $database and $ service as the arguments How do I proceed ? and how would a function be defined and these two arguments would be used inside the function? calc_pref_avail $database $service Best regards, Vishal (7 Replies)
Discussion started by: Vishal_dba
7 Replies

5. Shell Programming and Scripting

bash read within function with arguments

I have trouble getting this logic to work #!/bin/bash function assign_var(){ while do read -p "$2 :" $3 done } assign_var '$IPADDRESS' ipaddress IPADDRESS Basicly, i want to make sure that entry is made (i can add more sophisticated checks later), but the idea is to recycle... (11 Replies)
Discussion started by: serverchief
11 Replies

6. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

cat arguments to a function

Hi, I've a logging function in bourne shell, flog() which logs the first argument passed to it. How can I pass arguments to this function from a file, like cat filename | sed '...filtering...' | flog or cat filename | sed '...filtering...' | xargs flog Which did not work, after which... (3 Replies)
Discussion started by: Random_Net
3 Replies

8. Programming

Error: too many arguments to function 'sigwait'

#include <pthread.h> #include <signal.h> ... sigset_t mask; int err,signo; err=sigwait(&mask,&signo); switch(signo){ case SIGINT: ... } when I compile above code under solaris 10,it raise following error: error: too many arguments to function 'sigwait' I look up signal... (4 Replies)
Discussion started by: konvalo
4 Replies

9. Shell Programming and Scripting

no of arguments to function in shell script

Hi, I have a function in shell script fun1{ echo "No.of arguments are..."} this function will be called in same script by passing arguments fun 1 2 3 I want to check the no. of arguments passed to fun1 function in the same functionbefore validation. can any one suggest me. (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

10. Programming

sscanf function is failing

Please delete this thread. (0 Replies)
Discussion started by: jxh461
0 Replies
Login or Register to Ask a Question