There may be a smoother way with a single command, but otherwise how about putting the below into a tcsh file, such as "do_replacement.tcsh". You can replace the first four file names with the necessary information for you-- the input mask name and datafile name; and then the output mean- and std-replaced file names. Then, you can run it from the commandline with:
$ tcsh do_replacement.tcsh
At the moment, it will replace datafile voxels in the mask with either mean or std, *and* keep the values outside the mask what they originally were. You didn't specify in your post anything about the values outside the mask, so I left them unchanged. You can remove those if you want (-> mask them out) by getting rid of the "+ a * not(b)" part of the 3dcalc commands.
If you want, you can enter the file names from the commandline by replacing the four input/output file names ("MASK_NAME", "DATA_NAME", etc.) with $1, $2, $3, and $4 (literally, those two characters). Then, you can you the four file names you want as commandline arguments when you run the program, such as:
$ tcsh do_replacement.tcsh "MASK_NAME" "DATA_NAME" ...
That might be easier for scripting, if you wish.
--pt
-------------------------------- copy below into "do_replacement.tcsh" -----------------------------------
#/bin/tcsh
# input files: first mask, then data file
set my_mask = "MASK_NAME"
set my_datafile = "DATA_NAME"
# output files: first mean-replaced, then std-replaced one
set my_out_mean = "NEW_MEAN_NAME"
set my_out_std = "NEW_STD_NAME"
# ------------------------------------------------------------------
# calculate both mean and std
set A = `3dROIstats -quiet -sigma -mask $my_mask $my_datafile`
# separate out two returned values from above
set mask_mean = $A[1]
set mask_std = $A[2]
# replace values in mask with mean; keep values in datafile outside mask
3dcalc \
-a $my_datafile \
-b $my_mask \
-expr "$mask_mean * step(b) + a * not(b)" \
-prefix $my_out_mean \
-overwrite
# replace values in mask with mean; keep values in datafile outside mask
3dcalc \
-a $my_datafile \
-b $my_mask \
-expr "$mask_std * step(b) + a * not(b)" \
-prefix $my_out_std \
-overwrite