Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Write the a shell script program to remove all space characters stored in the shell variable TEXT. TEXT='onespace ,twospaces ,threespaces ,fourspaces ,'. Be sure to assign the results back to TEXT. Display the value of the TEXT variable to standard out. Do this using the tr command.

  2. Relevant commands, code, scripts, algorithms:
    tr

  3. The attempts at a solution (include all code and scripts):
    How do i go about writing a shell script program? If you can help me with that im hoping I can figure out how to answer the question.

  4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Calvin College, Grand Rapids MI, USA, Johnson, CO142.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

To delete spaces from a file:

tr -d ' ' < inputfile > outputfile

To do this to a string in the shell you must use echo and pipes.

1 Like

A shell program is simply a collection of commands. And when the commands are put together, the desired result is achieved.

The #1 tells you the variable TEXT and the command to be used tr.
What have you tried, what examples of work can you share?

1 Like

Thanks so much for the reply. I guess im still quite confused by the question. Am I creating a file with vi called "TEXT" with the content of "onespace, ect.."?

Props to you Linux guys who understand this stuff. I feel so lost in this class. Thanks for the help.

Also, you could

$ echo one two three >sample2.txt

$ cat sample2.txt
one two three

However, what you wrote did not say a file but a variable!

1 Like

i figured I wasnt putting it into a file.

You do not need to create a file necessarily. Did they tell you anything about backticks and pipes?

From what you originally wrote, I'm pretty sure you need to write a shell script:

Write the a shell script  program

In any case, it's better to use a shell script, because the problem has several steps.

So yes, you should use vi (or whatever editor you are comfortable with) to edit a file, and them submit your best guess here, and folks will give clues or help steer you in the right direction. Copy and paste from the file, and use the code tags, like I did above.

I would probably call the file tr_1.sh or something similar. Within the script, you will need to initialize the TEXT variable, remove the space characters from the variable, and then print out the variable.

To run the shell script, you'll need to set the file permissions correctly.