Search code examples
pythonpython-3.xbashfor-loopsh

how to run a command for many files


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


Solution

  • 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.