Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizPlease log in to access this content. You will be redirected to the login page shortly.
LoginGeneral Tech Bugs & Fixes 2 years ago
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.
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"
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
manpreet
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 withIt 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 ofprocess>
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 ofed
. I will then send each individual parts at the end of my randomized files.