简体   繁体   中英

Shell commands not working when grouped in a bash script

I have this file:

file.txt:
  4003f0:   48 83 ec 08             sub    $0x8,%rsp
  4003f4:   e8 73 00 00 00          callq  40046c

I am trying to extract the opcodes from the middle. First I search the first element after the opcodes (sub and callq):

get_asm_index.awk: 
BEGIN {
    i_min = 1000000;
}

{
    i = match($0, /[^ :0-9a-fA-F]/)
    if(i < i_min)
        i_min = i;
}

END {
    print i_min;
}

After that I print the opcodes:

get_hexdump.awk:
{
    i = match($0, /[:]/);
    print substr($0, i+1, i_min-i-1);
} 

Everything is working if I call the two awk files separately, but I get the following error when I group them in a shell script:

./do.sh: line 4: 48: command not found

The shell script:

do.sh:
#!/bin/bash
i_min=$(awk -f get_asm_index.awk file.txt)
$(awk -v i_min="$i_min" -f get_hexdump.awk file.txt)

Don't put the last command in $() . That will take stdout of the command within and put that in place of whatever is outside. So it could be used as value to set in a variable as in your line above, or it could be the spot where a parameter goes in a command line. In this case, you don't have anything else there, so you end up putting it in the spot where a command would go, and then the shell tries to execute it as a command.

In this example, it tries to execute 48 which is generally not going to be the name of a command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM