Home > Software design >  how to run a command for many files
how to run a command for many files

Time:01-25

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

CodePudding user response:

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.

CodePudding user response:

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