Having issues finishing up a few scripting problems. A little help would be awesome

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:

  2. Recall that the env command provides a list of various environment variables available to you. Two of those variables are PWD, which is your current directory, and HOME, which is your home directory. Write a script named script7.sh to test to see if you are currently in your home directory. This will be true if $PWD is equal to $HOME. Output the result.

  3. The following piece of code will loop through and print the files located in the current directory.

for file in $( ls )
do
echo $file
done

The condition [ -r $filename ] tests to see if $filename is readable. Write a script named script8.sh that uses a for loop to iterate through the current directory and test to see which files are readable. Output the names of all the readable files found in the directory. Include your script script8.sh as your answer to this problem.

  1. You can use the same logic that you used above by using the command cat $filename to output all of $filename.
for word in $( cat $filename )
do
echo $word
done 

Write a script named script9.sh where, in place of $filename, you pass in a parameter of a text file�s name. This will be referenced as $1 in the script. Pass in a second parameter, a string, which will be referenced as $2. Using the for loop with cat, count the number of occurrences of the string ($2) in the file. Output the string ($2) and the number of times you found it in the file. Test this out on a few different text files to make sure it works. Include your script script9.sh as your answer to this problem.

  1. Relevant commands, code, scripts, algorithms:
  2. gives a hint as to what I need to be looking at with:
for file in $( ls )
do
echo $file
done
  1. gives a hint as to what I need to be looking at as well
cat $filename to output all of $filename.  
for word in $( cat $filename )
do
echo $word
done 
  1. The attempts at a solution (include all code and scripts):
    I'm having issues even attempting a solution. I would appreciate even advanced help on these, or give examples like what it should be showing.

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Northern Kentucky University, KY, USA, Prof. Walker, CIT-130

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).

So what have you tried, and what is your question?
To be honest, for at least your first "question" (??) the solution is given by the task's question...

Try this, it might help:

  1. Make a script file with this content:
echo "$0 $1 $2 $3 $4"
echo "none $@"
  1. Call the script, lets call it test.sh , passing none, then 1, then 2, etc, words after its name and see what happens.
sh ./test.sh [word1 word2 ...]

Also, search (browse) through this forums 'Scripts & programming' threads, there are plenty of examples available.

Hope this helps you get started

They're not questions as much as "make this happen"

I think I figured out the middle question.

I'm having issues with the other two now.

PROBLEM #1:

Recall that the env command provides a list of various environment variables available to you. Two of those variables are PWD, which is your current directory, and HOME, which is your home directory. Write a script named script7.sh to test to see if you are currently in your home directory. This will be true if $PWD is equal to $HOME. Output the result.

PROBLEM #2:

#I'm given a code string of:

for word in $( cat $filename )
do
echo $word
done 

I'm directed to write a script named script9.sh where, in place of $filename, you pass in a parameter of a text file�s name. This will be referenced as $1 in the script. Pass in a second parameter, a string, which will be referenced as $2. Using the for loop with cat, count the number of occurrences of the string ($2) in the file. Output the string ($2) and the number of times you found it in the file. Test this out on a few different text files to make sure it works. Include your script script9.sh as your answer to this problem.

But remember - We are not here to do the work for you...

Have you tried typing env ?
What does the output look like?
What does pwd output?
What does echo $HOME output?
So?
in script9.sh You are asked to replace the "cat somefile" by "cat $1" and use a second parameter $2...
What cant you do here?
Show us what you did so far then and explain what is blocking you

How about this.

I've tried reading my book and googling. I'm not sure how to even use the "env" command. Let's begin by explaining in layman's terms and perhaps an example. I'm sure if it's explained with something for me to look at, I'll have a better understanding of how to do this.

I actually don't want it done for me. I'm in an accelerated class right now where we don't get ANY explanation of anything. I appreciate the help already though.

EDIT:

Here, I tried this, but it gives me a syntax error.

pwd
var=$ ( pwd )
if var = $HOME
then
echo you are in the home directory
else
echo you are not in the home directory

env is not really a program to use, but one to get infos from.
Actualy, the variables there show either what you have set in the shell, or have been loaded to the shell (by sourcing or exporting -- but thats, for the moment, beyond your needs).

As you dont it for yourself, why do it at all?

As of now, i might have one my moments, but i'm getting sick by people who cant even accomplish a simple test by their own.
Its nothing difficult, just DO IT and SEE what happens- that is called 'a learning process'... learning by doing...

To be mean, dont attend the accelerated class, but go to the basic class if you cant handle this (i'm even stunned this is called accelerated).
Heck, i'm not allowed in advanced classes because i dont have a 'basic' class visisted... reading this makes me sick...

And to be 'teachfull' nontheless... cat prints out all the content of the passed file.
Which in this case is the variable $filename , which you can set to ANYTHNG you like... like filename="$1"
The $( CODE ) around that command, executes the CODE and prints/execute it (depends on the rest of the line).
Do the above exmaple i told you, and you WILL understand... - if not, read again my 'mean' part...

EDIT2:
I wrote the 'mean' part as i didnt feel you were even following vbe's suggestions...

EDIT3:
Sorry if i was too harsch, might sound harder since i have to translate it to a foreign language...
I just wanted to make you do (show) more 'effort' of your own.

EDIT4:
Sorry didnt see you've edited your post...
You tried:

pwd
var=$ ( pwd )
if var = $HOME
then
echo you are in the home directory
else
echo you are not in the home directory

Lines:
1) pwd is a command and is executed
2) tries to set var to the output of pwd , fix the space issue, and it'll work
3) the condition misses encapsuling [ and ] , however i suggest to (get used to) use [[ and ]]
4) good
5) encapsule the STRING by quotes " , recomended, eventhough in this example it'll work this way...
6) good
7) see 5)
8) missing closing fi

Fix these issues and see what happens :slight_smile: