How to create files depending on operations of parameters

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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

dir=$3

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:

script.sh 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 process> in randoms files which will be in . Also would it be easier to process files in the meantime so they can be sorted alphabetically in ?

Example : script.sh 3 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.

profilepic.png
manpreet 2 years ago

 

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

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

In use:

$ ./go.sh
'Thi'
's w'
'ill'
' be'
' di'
'vid'
'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))}"

to

   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"

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.