Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

bash - sed: no input files

I have a script where I read in columns of data from an "input file", then use those to change some variables in another file.

Here is my script, titled FA_grid_changer.sh

#!/bin/bash

# before runnning, please ensure your inlist has the following parameters: RSP_mass, RSP_Teff, RSP_L, RSP_X, RSP_Z, 
# log_directory, photo_directory, RSP_alfa, photo_interval, profile_interval, history_interval, terminal_interval,
# max_num_profile_models, RSP_max_num_periods

export OMP_NUM_THREADS=8

# get current directory
dir=$PWD

data_file="$dir"/input.dat
mesa_inlist="$dir"/inlist_rsp_RR_Lyrae

# set STR to 1 for saving history and profile files BEFORE the model has reached full-amplitude pulsations
# set STR to 2 for saving history and profile files AFTER the model has reached full-amplitude pulsations
STR=1

max_num_periods=4000
max_num_periods_2=$(($max_num_periods + 4))

if [[ "$STR" = 1 ]]
then
    echo Configuring inlist to save settings before full-amplitude pulsations...
    sed -i 
        -e "s/^([[:blank:]]*photo_interval).*/1 = 1000/i" 
        -e "s/^([[:blank:]]*profile_interval).*/1 = 1000/i" 
        -e "s/^([[:blank:]]*history_interval).*/1 = 1000/i" 
        -e "s/^([[:blank:]]*terminal_interval).*/1 = 4000/i" 
        -e "s/^([[:blank:]]*max_num_profile_models).*/1 = 100/i" 
        -e "s/^([[:blank:]]*RSP_max_num_periods).*/1 = $max_num_periods/i" 
    "$mesa_inlist"
    sleep 1.5
    while read -ra fields; do
      echo Changing model parameters...
      sleep 0.5
      sed -i 
        -e "s/^([[:blank:]]*RSP_mass).*/1 = ${fields[3]}/i" 
        -e "s/^([[:blank:]]*RSP_Teff).*/1 = ${fields[5]}/i" 
        -e "s/^([[:blank:]]*RSP_L).*/1 = ${fields[4]}/i" 
        -e "s/^([[:blank:]]*RSP_X).*/1 = ${fields[2]}/i" 
        -e "s/^([[:blank:]]*RSP_Z).*/1 = ${fields[1]}/i" 
        -e "s/^([[:blank:]]*log_directory).*/1 = 'LOGS/LOGS_${fields[0]}'/i" 
        -e "s/^([[:blank:]]*photo_directory).*/1 = 'photos/photos_${fields[0]}'/i" 
        #-e "s/^([[:blank:]]*RSP_alfa).*/1 = ${fields[6]}/i" 
        "$mesa_inlist"
      echo Compiling MESA...
      sleep 0.5
      ./mk
      echo Running MESA...
      sleep 0.5
      ./rn
    done < "$data_file"
elif [[ "$STR" = 2 ]]
then
    echo Configuring inlist to save settings after full-amplitude pulsations...
    sed -i 
        -e "s/^([[:blank:]]*photo_interval).*/1 = 1000/i" 
        -e "s/^([[:blank:]]*profile_interval).*/1 = 1/i" 
        -e "s/^([[:blank:]]*history_interval).*/1 = 1/i" 
        -e "s/^([[:blank:]]*terminal_interval).*/1 = 4000/i" 
        -e "s/^([[:blank:]]*max_num_profile_models).*/1 = -1/i" 
        -e "s/^([[:blank:]]*RSP_max_num_periods).*/1 = $max_num_periods_2/i" 
    "$mesa_inlist"
    sleep 1.5
else
    echo STR: Not an acceptable option. Please set STR to 1 or 2.
fi

Here is my input file input.dat, which I am keeping in the same directory.

RRL1    0.001   0.749   0.61036165206037    48.3930329458203    6795.10018238295
RRL2    0.001   0.749   0.60627327846453    59.8125833648911    6793.11236483182
RRL3    0.001   0.749   0.606272337551755   56.5141274900899    7059.10474568471

And here is the file I am trying to change inlist_rsp_RR_Lyrae

!  long-period RR Lyrae star: M=0.65Ms, L=60Ls, Teff=6500K, X=0.75, Z=0.0004

! original test case contributed by Radek Smolec.

&star_job

      !pgstar_flag = .true.
   
/ ! end of star_job namelist


&controls

! check for retries and backups as part of test_suite
   !max_number_retries = -1
   !max_number_backups = 0
   !max_model_number = 15000

! RSP controls

  ! x_integer_ctrl(1) = 10 ! which period to check
   x_ctrl(1) = 0.71262d0 ! expected period (in days) 

   RSP_mass = 0.606272337551755
   RSP_Teff = 7059.10474568471
  !RSP_L = 59.3141274900899
   RSP_L = 56.5141274900899
   RSP_X = 0.749
   RSP_Z = 0.001
      
   RSP_max_num_periods = 4000
   RSP_nmodes = 10 ! number of modes LINA will calculate for initial model
   RSP_nz = 150 ! number of zones of model
   log_directory = 'LOGS/LOGS_M081_second'
   photo_directory = 'photos/photos_M081_second'

! output controls

      !num_trace_history_values = 2
      trace_history_value_name(1) = 'rel_E_err'
      trace_history_value_name(2) = 'log_rel_run_E_err'

   ! for cases in which you have a run that has reached steady pulses
   ! and you want to look at plots of just a few periods to see the details,
   ! i suggest the following method. interrupt the run soon after
   ! it makes a photo.  remove or delete LOGS/history.data to force
   ! creation of a new one when restart.  edit the inlist to set
   ! history_interval to 1 to get maximum time resolution.  
   ! restart the run and let it go for as many periods as desired.   

      do_history_file = .true.
      photo_interval = 1000
      profile_interval = 1000
      history_interval = 1000
      terminal_interval = 4000

      max_num_profile_models = 100

      !photo_interval = 1
      !profile_interval = 1
      !history_interval = 1
      !terminal_interval = 1

! FOR DEBUGGING
      !RSP_report_undercorrections = .true.
      !report_hydro_solver_progress = .true. ! set true to see info about newton iterations
      !report_ierr = .true. ! if true, produce terminal output when have some internal error
      !stop_for_bad_nums = .true.
      !trace_evolve = .true.

      ! hydro debugging
      !hydro_check_everything = .true.
      !hydro_inspectB_flag = .true.
      !hydro_sizequ_flag = .true.
      
      ! for making logs, uncomment hydro_dump_call_number plus the following
      ! to make residual logs, uncomment hydro_sizequ_flag
      ! to make correction logs, uncomment hydro_inspectB_flag
      ! to make jacobian logs, uncomment hydro_numerical_jacobian, hydro_save_numjac_plot_data
      ! to do dfridr test, uncomment hydro_get_a_numerical_partial, hydro_test_partials_k,
      !     hydro_numerical_jacobian, hydro_save_numjac_plot_data, hydro_dump_iter_number
         
      !hydro_get_a_numerical_partial = 1d-7
      !hydro_test_partials_k = 190
      !hydro_numerical_jacobian = .true.
      !hydro_save_numjac_plot_data = .true.
      !hydro_dump_call_number = 1
      !hydro_dump_iter_number = 6
      
      !hydro_epsder_struct = 1d-6
      !hydro_epsder_chem = 1d-6
      !hydro_save_photo = .true. ! Saves a photo when hydro_call_number = hydro_dump_call_number -1

      !fill_arrays_with_NaNs = .true.

      !report_why_dt_limits = .true.
      !report_all_dt_limits = .true.
      !report_hydro_dt_info = .true.
      !report_dX_nuc_drop_dt_limits = .true.
      !report_bad_negative_xa = .true.
      
      !show_mesh_changes = .true.
      !mesh_dump_call_number = 95
      

      !trace_newton_bcyclic_solve_input = .true. ! input is "B" j k iter B(j,k)
      !trace_newton_bcyclic_solve_output = .true. ! output is "X" j k iter X(j,k)

      !trace_newton_bcyclic_matrix_input = .true.
      !trace_newton_bcyclic_matrix_output = .true.
      
      !trace_newton_bcyclic_steplo = 1 ! 1st model number to trace
      !trace_newton_bcyclic_stephi = 1 ! last model number to trace
      
      !trace_newton_bcyclic_iterlo = 2 ! 1st newton iter to trace
      !trace_newton_bcyclic_iterhi = 2 ! last newton iter to trace
      
      !trace_newton_bcyclic_nzlo = 1 ! 1st cell to trace
      !trace_newton_bcyclic_nzhi = 10000 ! last cell to trace; if < 0, then use nz as nzhi
      
      !trace_newton_bcyclic_jlo = 1 ! 1st var to trace
      !trace_newton_bcyclic_jhi = 100 ! last var to trace; if < 0, then use nvar as jhi
      
      !trace_k = 0
      

/ ! end of controls namelist



&pgstar

      !pause = .true.

      pgstar_interval = 6
         
      Grid2_win_flag = .true.

      Grid2_title = '4.165 Md(2281)u  Z=0.007  Classical Cepheid'
      
      History_Panels1_xaxis_name='star_age_day'
      History_Panels1_max_width = 365 ! only used if > 0.  causes xmin to move with xmax.

      !  Grid2_file_flag = .true.
      file_digits = 7
      Grid2_file_dir = 'png'
      Grid2_file_prefix = 'grid'
      Grid2_file_interval = 5 ! output when mod(model_number,Grid2_file_interval)==0
      
      !Profile_Panels1_show_grid = .true.

      Profile_Panels1_xaxis_name = 'logtau'
      Profile_Panels1_xaxis_reversed = .true.
      Profile_Panels1_xmin = -101D0
      Profile_Panels1_xmax = -101D0

      Profile_Panels1_dymin(4) = 0.02
      Profile_Panels1_yaxis_name(2) = 'avg_charge_He'         

/ ! end of pgstar namelist

Yet, when I try to run the shell script in bash, inlist_rsp_RR_Lyrae remains unchanged. Instead, this occurs:

Configuring inlist to save settings before full-amplitude pulsations...
Changing model parameters...
sed: no input files
/home/nick/mesa-r11701/star/test_suite/rsp_RR_Lyrae_grid/inlist_rsp_RR_Lyrae: line 1: long-period: command not found
/home/nick/mesa-r11701/star/test_suite/rsp_RR_Lyrae_grid/inlist_rsp_RR_Lyrae: line 3: original: command not found
/home/nick/mesa-r11701/star/test_suite/rsp_RR_Lyrae_grid/inlist_rsp_RR_Lyrae: line 5: syntax error near unexpected token `&'
/home/nick/mesa-r11701/star/test_suite/rsp_RR_Lyrae_grid/inlist_rsp_RR_Lyrae: line 5: `&star_job'
Compiling MESA...

As you can see, it is mistaking the first few lines of comments in inlist_rsp_RR_Lyrae as commands. Is there something I am missing here?

question from:https://stackoverflow.com/questions/65893225/sed-no-input-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is

sed -i 
  ...
  -e "s/^([[:blank:]]*photo_directory).*/1 = 'photos/photos_${fields[0]}'/i"  
  #-e "s/^([[:blank:]]*RSP_alfa).*/1 = ${fields[6]}/i" 
  "$mesa_inlist"

Here the comment before $mesa_inlist also comments out the at the end of the line. Therefore the command ends at that comment and sed -i complains that there is no file given. Afterwards bash tries to execute "$mesa_inlist" (that is inlist_rsp_RR_Lyrae) as a command, which fails.

To fix the problem, simply remove the comment line.


Bye the way: With this function you can drastically improve the readability of your script.

sedi() {
 mapfile -t args < <(printf -- '-e
s/^([[:blank:]]*%s).*/\1 = %s/i
' "$@")
 sed -i "${args[@]}" "$mesa_inlist"
}

Now you can replace ...

sed -i 
        -e "s/^([[:blank:]]*photo_interval).*/1 = 1000/i" 
        -e "s/^([[:blank:]]*profile_interval).*/1 = 1/i" 
        -e "s/^([[:blank:]]*history_interval).*/1 = 1/i" 
        -e "s/^([[:blank:]]*terminal_interval).*/1 = 4000/i" 
        -e "s/^([[:blank:]]*max_num_profile_models).*/1 = -1/i" 
        -e "s/^([[:blank:]]*RSP_max_num_periods).*/1 = $max_num_periods_2/i" 
    "$mesa_inlist"

... by ...

sedi 
  photo_interval 1000 
  profile_interval 1 
  history_interval 1 
  terminal_interval 4000 
  max_num_profile_models -1 
  RSP_max_num_periods "$max_num_periods_2"

... and so on for the other sed commands.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...