0

This is only a little piece of code... but the terminal give me an error "syntax error near token not expected "archive" ". The functions in bash do not accept parameters like function fun (hello) {

function extractProcess (archive){
  mv $archive $WORK_DIR
  pathFile=${archive%/*} #Path input archive
  nameFile=$(ls $WORK_DIR)
  name=${archive%.*} #nameFile without exteension
  case $nameFile in
    *.tar.bz)   tar xjf "$WORK_DIR/$nameFile" -C $WORK_DIR          ;;
    *.tar.bz2)  tar xjf "$WORK_DIR/$nameFile" -C $WORK_DIR          ;;
    *.tar.gz)   tar xzf "$WORK_DIR/$nameFile" -C $WORK_DIR          ;;
    *.bz)       bunzip2 "$WORK_DIR/$nameFile"      ;;
    *.bz2)      bunzip2 "$WORK_DIR/$nameFile"      ;;
    *.gz)       gunzip -Ndk "$WORK_DIR/$nameFile"       ;;
    *.tar)      tar xf "$WORK_DIR/$nameFile" -C $WORK_DIR       ;;
    *.tbz)      tar xjf "$WORK_DIR/$nameFile" -C $WORK_DIR      ;;
    *.tbz2)     tar xjf "$WORK_DIR/$nameFile" -C $WORK_DIR      ;;
    *.tgz)      tar xzf "$WORK_DIR/$nameFile" -C $WORK_DIR      ;;
    *.zip)      unzip -qq "$WORK_DIR/$nameFile" -d $WORK_DIR        ;;
  esac
  • 1
    [shellcheck](https://www.shellcheck.net/) says: "Trying to declare parameters? Don't. Use () and refer to params as $1, $2.." – that other guy Apr 19 '18 at 17:56
  • The rest of shellcheck's warnings (many of which are only displayed after more immediate problems are fixed) are also pertinent. See also [Why you shouldn't parse the output of `ls`](http://mywiki.wooledge.org/ParsingLs). – Charles Duffy Apr 19 '18 at 18:04
  • Your title refers to "input parameters", but it's not clear what you mean by that phrase. Bash doesn't have anything it formally refers to by that term. – Charles Duffy Apr 19 '18 at 18:26

2 Answers2

0

You can, but you cannot declare formal parameters. Instead, whatever arguments are passed when you call the function are available as positional parameters $1, $2, etc in the body.

# The parentheses are always empty, to signal a function definition.
# The function keyword is a non-standard extension and should be avoided.
extractProcess () {

    archive=$1
    ...
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This function calls me sevre to be called in a for loop, and the index of this for loop is the parameter that I have to pass in input – Andrea Ghezzi Apr 19 '18 at 18:05
  • @AndreaGhezzi, how/why do you think that makes this answer invalid? `$1`, in a function, is the first argument *passed to the function*; it no longer refers to the first argument passed to the script as a whole. Thus, you accept arguments in functions the same way you accept arguments in scripts. – Charles Duffy Apr 19 '18 at 18:12
  • @CharlesDuffy can not I pass an input parameter to the function? that then for example as in my case I recall inside the for and use parameter as the parameter of the for – Andrea Ghezzi Apr 19 '18 at 18:19
  • @AndreaGhezzi, what do you mean "input parameter"? Do you mean a parameter that was passed positionally to the script? If you want to pass that through, do so explicitly: `extractProcess "$1" "$archive" "$workDir"` will pass the original `$1`, before the variables `archive` and `workDir`. – Charles Duffy Apr 19 '18 at 18:22
  • @AndreaGhezzi, ...or if you want to pass *all* of your script's parameters through to the function, you can of course use `"$@"` for that. If you call `yourFunction "$someVar" "$@"`, then inside the function `"$someVar"` will be `$1`, then the original `$1` will be `$2`, the original `$2` will be `$3`, etc. – Charles Duffy Apr 19 '18 at 18:23
  • @CharlesDuffy in this way, invoke the extractProcess function and pass it as indicated by the index of the per, in this case $ f for ext in ${EXTENSIONS[@]} do ARCHIVES=$(find $d -name "*.$ext") #Inizialize archives for f in "${ARCHIVES[@]}" do extractProcess($f) done done – Andrea Ghezzi Apr 19 '18 at 18:39
  • @AndreaGhezzi It's just `extractProcess "$f"`; inside the body of `extractProcess`, `$1` refers to the value passed from `$f`. – chepner Apr 19 '18 at 19:29
  • @AndreaGhezzi, that code is inherently buggy -- you can't safely store a list of filenames in a string-type variable, since any character that can be in a string can also be in a filename (so there's no possible way to know with safety where one name ends and the next begins). See [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29), and the "Actions" and "Complex Actions" sections of [Using Find](http://mywiki.wooledge.org/UsingFind) for alternate (safe) practices. – Charles Duffy Apr 19 '18 at 20:01
  • @AndreaGhezzi, ...let me reiterate the note in the comment on the question advising you to run your code through http://shellcheck.net/. – Charles Duffy Apr 19 '18 at 20:02
-1

Rather than trying to define them in the function declaration, use the $ parameters:

function extractProcess (){
  archive=$1
  mv $archive $WORK_DIR
  pathFile=${archive%/*}
bedwyr
  • 5,774
  • 4
  • 31
  • 49
  • 1
    `function foo() {` merges ksh and POSIX sh function declaration forms into a result that's strictly compatible with neither of those predecessor shells. Use either `function foo {` for compatibility with ancient ksh, or `foo() {` with no preceding `function` for compliance with the POSIX sh standard (and thus compatibility with dash, ash, and all other POSIX-superset shells). See also http://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Apr 19 '18 at 17:59
  • Also, please try to show correct quoting in your examples -- ie. `mv "$archive" "$WORK_DIR"` (or, even better, `mv -- "$archive" "$WORK_DIR"`, to support names that start with a dash). – Charles Duffy Apr 19 '18 at 18:02
  • This function calls me sevre to be called in a for loop, and the index of this for loop is the parameter that I have to pass in input – Andrea Ghezzi Apr 19 '18 at 18:05
  • @CharlesDuffy, good points all. I wrote this off-the-cuff (copy/paste) yesterday w/out thinking too much about quoting or using the `function` keyword... – bedwyr Apr 20 '18 at 18:22