0

I'm pretty new to unix and I'm wondering how I would go about writing a word, letter by letter into my program's memory. So for example if my word was "cup", I'd want an array A to have A[0] = 'c', A[1] = 'u', A[2] = 'p'. I've tried looking this up but couldn't find anything clear enough, sorry if this is a stupid question but I just don't know what to do. I'm using BASH for this program.

At the end I plan on just making a loop to perform this task for me but I just need to find out how to actually create the array and write a character to each index.

I appreciate any help at all, thank you.

jayhendren
  • 8,384
  • 2
  • 33
  • 58
  • What scripting/programming language are you using? – jayhendren Dec 06 '16 at 00:31
  • Oops sorry about that, bash. – TonyIsMyName Dec 06 '16 at 00:37
  • See my answer here and also the linked answer from there. Bash isn't really a programming language the way you think it is. – Wildcard Dec 06 '16 at 00:44
  • To quote Stephane Chazelas, "In C or most other languages, building blocks are just one level above computer instructions. You tell your processor what to do and then what to do next. You take your processor by the hand and micro-manage it: you open that file, you read that many bytes, you do this, you do that with it.

    "Shells are a higher level language. One may say it's not even a language. They're before all command line interpreters. The job is done by those commands you run and the shell is only meant to orchestrate them."

    – Wildcard Dec 06 '16 at 00:45
  • Oh I really don't want to be using this but it's for school, have no choice :/ – TonyIsMyName Dec 06 '16 at 00:46
  • For this assignment we have to enter a bunch of numbers and sort them a specific way. One of the steps is to write it to memory though, which I don't know how to do yet. Using Bash for this was definitely not my choice :P – TonyIsMyName Dec 06 '16 at 00:49
  • Bash is a fabulous "language" when used for its intended purpose (orchestrating all the existing executable tools on your computer.) If you're trying to write a word letter by letter into memory using Bash, you're doing something wrong. If you're going to do that using a shell loop to process text, you're doing something wronger. My sympathies about the school assignment, but you may want to clarify what the overall assignment really is and the restrictions. – Wildcard Dec 06 '16 at 00:49
  • Exact words from the assignment: "Copy the letters by a loop one character at a time into the program's memory (Store in a byte array)." Why we're being made to do this, I have absolutely no idea. – TonyIsMyName Dec 06 '16 at 00:51
  • Perhaps you're supposed to do that using C, and then compile and run the C program from Bash (i.e. from the command line rather than using a GUI compilation tool.) That's an assignment I would agree with 100%. Try doing that. – Wildcard Dec 06 '16 at 00:55
  • We haven't even been taught C :P We were barely even taught Bash. This is honestly a pathetic course. We have to make this entirely in Bash – TonyIsMyName Dec 06 '16 at 00:56
  • (P.S.: There's no such thing as a "byte array" in Bash; all variables are strings.) Well, read through in its entirety the answer I linked, wherein at least you'll learn a lot about what Bash is really for, and then based on your new understanding consider what you should do; perhaps asking your professor for a clarification. (No Bash script is "entirely in Bash" from a literal standpoint; all Bash scripts worth writing call external tools.) – Wildcard Dec 06 '16 at 01:00
  • That was an exact copy from the assignment, not surprised that my prof wrote that. And this part was supposed to be made with Bash, haven't gotten to the next few parts but they're to be made with NASM so assembly. Sorry for leaving that part out, but thank you for the link I'll look into it. – TonyIsMyName Dec 06 '16 at 01:05
  • In nasm it's A: db "cup" or something like that. - probably best to start a new question. – Jasen Dec 06 '16 at 04:16
  • Alright thanks Jasen I'll try it out and do that if needed – TonyIsMyName Dec 06 '16 at 04:41

1 Answers1

0

Something like this works fine in my bash:

readarray word < <(echo "$resp" |fold -w1)    

Complete script/exercise:

read -p "Give me a word:  " resp
readarray letter < <(echo "$resp" |fold -w1)
for ((i=0;i<${#letter[@]};i++)); do
    echo "letter[$i] : ${letter[$i]}"
done