1

I have two function first one call usage -file to get the file from the user. It has long filename, so this function strips just what I need. For example: ./test.sh -file some_example_list_data2017.csv It give me data2017 (which is what I want).

function get_file_name() {
    filename=$1
    if [ -f $filename ]
    then 
        echo "${filename}" | awk (make long filename short)
    else 
        echo "${filename} not found"
    fi
}

Second function: I want to pass the above $filename here, since it already has full filename. And I want to display only some content on the screen. But this time using ./test.sh -display someexample_list_data2017.csv

function parse_above_file() {
    // here I want get_file_name and display the content
    filename=$1
    echo ${filename} | awk/sed/egrep (do something and display)
}

But second function isn't getting the filename passed. When it ran the script it just prints nothing.

  • Don't you want to do just this in your `get_file_name` after getting the short name: `if ... then parse_above_file "$(echo "${filename}" | awk makeshort ... )"` ? – PesaThe Dec 06 '17 at 23:39
  • I want to pass the whatever filename variable is holding after get_file_name() gets. And print/do-something at parse_above_file() using same data (that $filename has) – user3010071 Dec 06 '17 at 23:43
  • This is probably the way you want to call the functions: `parse_above_file "$(get_file_name "$full_filename")"` - you pass the result of one function `get_file_name` to another function `parse_above_file`. – PesaThe Dec 06 '17 at 23:45
  • Done that. terminal does nothing but waits there. thank tho – user3010071 Dec 06 '17 at 23:52
  • 1
    You may want to give us more code. – PesaThe Dec 06 '17 at 23:56
  • Assign the output of `get_file_name()` to a variable, then use that variable as an argument to `parse_above_file()`. – Barmar Dec 06 '17 at 23:57
  • Just to confirm, you are not expecting to call the script at the command line twice with 2 different switches and expect the filename variable to still be populated from the first run? – grail Dec 07 '17 at 02:42
  • No Sir. I will be calling at different times. ./test.sh -f filename and other is ./test.sh -r filename. – user3010071 Dec 07 '17 at 02:56
  • If you don't declare your variables local, they're shared across all scopes. `local filename` if you don't want that. – Charles Duffy Dec 07 '17 at 03:25

1 Answers1

0

With this code,

function get_file_name() {
    filename=$1
    echo "$filename"
    }
function parse_above_file() {
    filename=$1
    echo "$filename"
    filename="bli"
    get_file_name "$filename"
    exit
    }
filename="bla"
get_file_name $filename
filename="ble"
parse_above_file $filename

I get this output:

bla
ble
bli
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
user1747036
  • 524
  • 4
  • 12
  • Our markdown isn't GitHub-flavored -- triple quotes don't mean anything different than single quotes do. Four-space indents, as created by selecting a code block and clicking the `{}` button, are the proper way to format multi-line code segments (with syntax highlighting, block formatting, &c). – Charles Duffy Dec 07 '17 at 03:26
  • 1
    btw, consider leaving out the `function` keyword -- it adds no value but breaks compatibility with POSIX shells. `get_file_name() {` is sufficient to constitute a declaration. – Charles Duffy Dec 07 '17 at 03:27
  • 2
    Also, locals should be declared as such (as with `local filename` inside your function). Otherwise, when you run `filename=$1` inside a function, you're redefining the globally-scoped `filename` variable, not defining a local. – Charles Duffy Dec 07 '17 at 03:28
  • Quote the `$filename` even when calling the function :) – PesaThe Dec 07 '17 at 12:25