0

I have installed a software in linux machine and it is accessible through the command line.

The command is mkrun and when i enter mkrun in the terminal it ask 4 user inputs like

run_type
run_mode
mode_type
cat additional_file #additional_file is .txt file

I want to use the mkrun command for many files of a directory.However for single file i am able to run the programme by executing the

mkrun << END
$run_type
$run_mode
$mod_type
$(cat $additional_file)
END

But when i am trying the same comand for many files by incorporating it in the loop it doesnot work

    #!/bin/sh
    run_type=4
    run_mode=2
    mod_type=3
    for additional_file in *.txt
    do
      mkrun << END
      $run_type
      $run_mode
      $mod_type
      $(cat $additional_file)
    END
    done

I think problem with END. can anybody suggest me a better solution for the same.

Error is: warning: here-document at line 12 delimited by end-of-file (wanted `END') syntax error: unexpected end of file

pro
  • 113
  • 8
  • The end word for a heredoc **must not** have any leading/trailing whitespace -- "END" must be the only characters on the line. – glenn jackman Jan 24 '23 at 16:04
  • Also, the `#!/bin/sh` [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) must not be indented; the first two bytes of the file need to be `#` and `!` in order for the kernel to recognize it as a valid shebang. – tripleee Jan 26 '23 at 05:58

2 Answers2

2

By the way you can use array for your additional_file to start

Also you can provide function with your commands.

Example with array:

#!/bin/sh

# Array
additional_files=('File_1' 'File_2' 'File_3')

# Function
commands(){
    command_1
    command_2
    command_3
    command_4
}

for file in ${additional_files[@]}; do
    commands                      
done
macder
  • 145
  • 9
1

If your heredoc is indented, you can add a dash as an option like this and it will suppress leading TAB characters (but not spaces):

  mkrun <<-END
  TAB$run_type
  TAB$run_mode
  TAB$mod_type
  TAB$(cat $additional_file)
END

You can equally try:

{ echo $run_type; echo $run_mode; echo $mod_type; cat "$additional_file"; } | mkrun

Don't be tempted to omit any spaces or semi-colons in the above command.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432