Bash sed:没有输入文件

Bash sed:没有输入文件,bash,sed,Bash,Sed,我有一个脚本,我从一个“输入文件”中读取数据列,然后使用这些来更改另一个文件中的一些变量 这是我的脚本,标题为FA\u grid\u 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

我有一个脚本,我从一个“输入文件”中读取数据列,然后使用这些来更改另一个文件中的一些变量

这是我的脚本,标题为
FA\u grid\u 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
这是我试图更改的文件
inlist\u rsp\u RR\u 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 M\d\(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

如您所见,它将列表中的前几行注释误认为是命令。这里有我遗漏的东西吗?

问题是

sed -i \
  ...
  -e "s/^\([[:blank:]]*photo_directory\).*/\1 = 'photos\/photos_${fields[0]}'/i" \ 
  #-e "s/^\([[:blank:]]*RSP_alfa\).*/\1 = ${fields[6]}/i" \
  "$mesa_inlist"
在这里,列表中
$mesa\u之前的注释也注释掉了行末尾的
\
。因此,命令在该注释处结束,并且
sed-i
抱怨没有给出任何文件。之后,
bash
尝试执行
“$mesa\u inlist”
(即
inlist\u rsp\u RR\u Lyrae
)作为命令,但失败

要解决此问题,只需删除注释行


顺便说一句:使用此功能,您可以显著提高脚本的可读性

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

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"
。。。其他
sed
命令也是如此。

问题是

sed -i \
  ...
  -e "s/^\([[:blank:]]*photo_directory\).*/\1 = 'photos\/photos_${fields[0]}'/i" \ 
  #-e "s/^\([[:blank:]]*RSP_alfa\).*/\1 = ${fields[6]}/i" \
  "$mesa_inlist"
在这里,列表中
$mesa\u之前的注释也注释掉了行末尾的
\
。因此,命令在该注释处结束,并且
sed-i
抱怨没有给出任何文件。之后,
bash
尝试执行
“$mesa\u inlist”
(即
inlist\u rsp\u RR\u Lyrae
)作为命令,但失败

要解决此问题,只需删除注释行


顺便说一句:使用此功能,您可以显著提高脚本的可读性

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

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"
。。。对于其他
sed
命令,依此类推