Need to pass variable in a command and assign value to a variable

Hello All,

Hope you're doing well !

I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted.

header_date_14=$(m_dump /wload/baot/home/baotasa0/UKRB_UKBE/sandboxes/EXTRACTS/UK/RB/UKBA/ukrb_ukba_pbe_acq/dml/ukrb_ukba_acnt_bde27_src.dml $1 | head -35)

Any pointers will be greatly helpful !

Thank you !
Ekta

  • execute the command on its own
  • set the xtrace option ( set -vx )
    and post the results.

You should never NEVER use constructs like $1 directly in your code. You see, $1 , $2 , etc. are positional parameters, not variables. $1 isn't the name of a variable just happening to have the name "1", like "$foo", it is the first parameter passed to this specific context - and what "this specific context" means can change (and does so) from context to context.

Therefore instead of doing:

var=$( command $1 )

do it like this:

argument1=$1
argument2=$2

...

var=$( command $argument1 $argument2 )

This also gives you the opportunity to give a meaningful name to the variable like "inputfile", instead of "$1", which won't tell you by its name what it is supposed to contain.

I hope this helps.

bakunin

Below is the command, which is used for accessing multi files, what I was trying to do is to pass the name of the file as an argument, which is highlighted below -

m_dump /wload/baot/home/baotasa0/UKRB_UKBE/sandboxes/EXTRACTS/UK/RB/UKBA/ukrb_ukba_pbe_acq/dml/ukrb_ukba_acnt_bde27_src.dml bdfo27m_a016_19122014_V1.dat | head -35

This should give me something similar to what is mentioned below -
Record 1:

[record
  bdfo_len      73
  bdfo_key      "     200000016"
  bdfo_param_no "000"
  parm01        NULL
  parm02        NULL
  parm03        NULL
  parm04        NULL
  parm05        NULL
  parm06        NULL
  parm07        NULL
  parm08        NULL
  parm09        NULL
  parm13        NULL
  parm16        NULL
  parm017       NULL
  parm18        NULL
  parm93        NULL
  parm94        NULL
  parm96        NULL
  parm97        NULL
  parm83        NULL
  header        [record
                  bdfo_run_date     41991
                  bdfo_file_id      "GYFL3027"
                  bdfo_centre_id    "G"
                  bdfo_sbi_file_ind "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"]
  trailer       NULL
  data          NULL]

I need to extract the bdfo_run_date and display it...

Any help will be greatly appreciated !

Thank you in advance

This request is different from the one in post#1. You didn't give us any hint on what might go wrong with your approach. The head -35 obviously prints mere 29 lines. IS anything ELSE going wrong? SHOULD this give you something similar, or DOES it?

Hello both,

Thank you for writing back, the last line i.e. head -35 should return with a record like I am mentioning below - Record 1:

[record
  bdfo_len      73
  bdfo_key      "     200000016"
  bdfo_param_no "000"
  parm01        NULL
  parm02        NULL
  parm03        NULL
  parm04        NULL
  parm05        NULL
  parm06        NULL
  parm07        NULL
  parm08        NULL
  parm09        NULL
  parm13        NULL
  parm16        NULL
  parm017       NULL
  parm18        NULL
  parm93        NULL
  parm94        NULL
  parm96        NULL
  parm97        NULL
  parm83        NULL
  header        [record
                  bdfo_run_date     41991
                  bdfo_file_id      "GYFL3027"
                  bdfo_centre_id    "G"
                  bdfo_sbi_file_ind "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"]
  trailer       NULL
  data          NULL]

which is of 35 lines and constitues to first record of the file we are trying to read using m_dump, I need to place the file name using command line and as per Bakunin's suggestion I tried using below code but it did not work -

param1 = $inputfile1
param2  = $inputfile2
param3 = $inputfile3
header_date_14=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $'$param1' | head -35)
header_date_15=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $'$param2' | head -35)
header_date_16=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $'$param3' | head -35)

Once this command is executed, I need to take out bdfo_run_date 41991 to display it...

Thank you in advance !

Neither of my questions has been answered, none of my suggestions was applied, helpful info was not supplied. Instead of focusing on one problem (specified in the first post), you are broadening the operating field and introduce new errors. In your last post, there are at least two syntactical errors: spaces around the equals signs when assigning shell variables, and single quotes around the variables preventing them from being expanded. No surprise it doesn't do what you expect.
Unless you supply info enabling people to help you, I'm afraid I can't help further.

First: "did not work" is NOT an analysis - it is like calling the doctor and then tell him "it hurts". It might be somewhat relevant to know what "it" is...

Second: RudiC already told you that spaces around equal signs are a syntax error. You sould have gotten some diagnostic message to that effect, no?

Instead of

param1 = $inputfile1

do

param1=$inputfile1

and better yet, because even this will fail if $inputfile contains white space, do

param1="$inputfile1"

Third: I don't know what you are trying to do with:

header_date_14=$(m_dump /some/file  $'$param1' | head -35)

but it is not gonna work. The single quotes are preventing the expansion of the variable $param1 (actually this is what their purpose is) and therefore you don't have the value of $param1 there, but the literal string "$param1". This is most probably not what you wanted. Instead write:

header_date_14=$(m_dump /some/file  "$param1" | head -35)

Again, the double quotes prevent the command from failing if a filename contains spaces.

I hope this helps.

bakunin

1 Like

Apologies if I confused you, I tried executing command with set -vx option and it did not yield any result, I did not pass command line argument i.e. $1 as I tried in my previous post rather used something like

m_dump /wload/baot/home/baotasa0/UKRB_UKBE/sandboxes/EXTRACTS/UK/RB/UKBA/ukrb_ukba_pbe_acq/dml/ukrb_ukba_acnt_bde27_src.dml bdfo27m_a016_19122014_V1.dat | head -35

Now this command brings up a record having value which is below, I need to extract any random record and look for the date, once the date is received that needs to be displayed -

[record
  bdfo_len      73
  bdfo_key      "     200000016"
  bdfo_param_no "000"
  parm01        NULL
  parm02        NULL
  parm03        NULL
  parm04        NULL
  parm05        NULL
  parm06        NULL
  parm07        NULL
  parm08        NULL
  parm09        NULL
  parm13        NULL
  parm16        NULL
  parm017       NULL
  parm18        NULL
  parm93        NULL
  parm94        NULL
  parm96        NULL
  parm97        NULL
  parm83        NULL
  header        [record
                  bdfo_run_date     41991
                  bdfo_file_id      "GYFL3027"
                  bdfo_centre_id    "G"
                  bdfo_sbi_file_ind "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"]
  trailer       NULL
  data          NULL]