See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

How to create files depending on operations of parameters

General Tech Bugs & Fixes
Max. 2000 characters

James Watson


( 7 months ago )


I have to take the $4 parameter, divide it equally by $2 parameter (If it cant be divided equally, add spaces to fill last part) and put every part at the end of randomized files we just created with


mkdir -p -- "$dir" || exit 1

fname=$(shuf -n 1 "$1")

tr -dc 'A-Za-z0-9' </dev/urandom | head -c 255 >"$dir/$fname"

It looks like this: <word_src> <size_of_parts> <out_dir> <text_to_process>.

The number of files created depends on how many divided parts of the $4 by $2. Basically I need to equally hide part of <test_to_process> in randoms files which will be in <out_dir>. Also would it be easier to process files in the meantime so they can be sorted alphabetically in <out_dir>?

Example : <word_src> 3 <out_dir> this will be divided .There "this will be divided" will be equally seperated in parts of 3 characters "thi" "s w" "ill" " be" " di" "vid" "ed ". In this example I needed to add a space at the end of ed. I will then send each individual parts at the end of my randomized files.

Mike Franklin


( 7 months ago )


This routine should solve your string-splitting task. Adapt to your needs:

$ cat
#!/usr/bin/env bash
str="This will be divided"
while [[ $ptr -le ${#str} ]]; do
   printf "'%-3s'\n" "${str:$((ptr)):$((interval))}"

In use:

$ ./
's w'
' be'
' di'
'ed '

This uses bash's built in parameter expansion tooling for substring extraction. Given a variable foo:

  • $foo is replaced with the contents of foo
  • ${#foo} is replaced with the number of items in foo. For an array, this is the number of assigned values; for a non-array, this is the length of the string represented by $foo (foo=10; echo ${#foo} yields 2).
  • ${foo:4:4} is replaced with the contents of foo, starting at the fifth character (zero is the first index), for four characters.

The last of these is the key here: We use our string, a pointer for how many characters we've printed so far, and a defined interval.

We combine this with formated strings: %-3s will output a left-justified, space-padded string. So if we provide only one or two characters, space padding is added to the right for us.

To write to files, you can simply change

   printf "'%-3s'\n" "${str:$((ptr)):$((interval))}"


   fname="$dir/$(shuf -n 1 "$1")"
   tr -dc 'A-Za-z0-9' </dev/urandom | head -c 255 > "$fname"
   printf "'%-3s'\n" "${str:$((ptr)):$((interval))}" >> "$fname"

what's your interest