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

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Having issues finishing up a few scripting problems. A little help would be awesome
# 1  
Old 10-05-2014
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:

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

5. The following piece of code will loop through and print the files located in the current directory.
Code:
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.

6. You can use the same logic that you used above by using the command cat $filename to output all of $filename.
Code:
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.


2. Relevant commands, code, scripts, algorithms:
5. gives a hint as to what I need to be looking at with:

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

6. gives a hint as to what I need to be looking at as well

Code:
cat $filename to output all of $filename.  
for word in $( cat $filename )
do
echo $word
done


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


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

Last edited by Scrutinizer; 10-05-2014 at 04:40 PM.. Reason: code tags
# 2  
Old 10-05-2014
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:
Code:
echo "$0 $1 $2 $3 $4"
echo "none $@"

2. Call the script, lets call it test.sh, passing none, then 1, then 2, etc, words after its name and see what happens.
Code:
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

Last edited by sea; 10-05-2014 at 05:06 PM..
# 3  
Old 10-05-2014
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:
Code:
#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.

Last edited by vbe; 10-05-2014 at 05:54 PM.. Reason: code tags
# 4  
Old 10-05-2014
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
# 5  
Old 10-05-2014
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.
Code:
pwd
var=$ ( pwd )
if var = $HOME
then
echo you are in the home directory
else
echo you are not in the home directory



Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 10-06-2014 at 05:17 AM.. Reason: code tags!!
# 6  
Old 10-05-2014
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:
Code:
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 Smilie

Last edited by sea; 10-05-2014 at 07:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues with awk scripting for HTML tags

Below is the data file(results) contents- 13450708,13470474,US,15 24954,24845,JPN,44 14258992,14365059,US,4 24954,24845,IND,44 I want to send above data sets to email in a tabular format. For that I am using below awk script. Now the challenge I am facing here is - I want to make the... (2 Replies)
Discussion started by: Roseline
2 Replies

2. Shell Programming and Scripting

Issues with scripting users

Hello everyone, I am new to the forum community and need help with some scripts. First off let me put that this is “NOT” a homework assignment as I have been given this information where I work (IUPUI-UITS) and need help on this since I have no experience in this area. The only reason I am turning... (1 Reply)
Discussion started by: wingzero89
1 Replies

3. Shell Programming and Scripting

Scripting users issues...

Hello everyone, I am new to the forum community and need help with some scripts. I have attached what I have so far and these are scripts that have been passed on to me for a project that I have been assigned. To tell the truth I have never really worked with scripts and have no clue how these even... (3 Replies)
Discussion started by: wingzero89
3 Replies

4. Shell Programming and Scripting

Unix scripting problems

Hi, In my unix server, i received a file in /usr/data/xmit location. i want to write a unix script after file reached. So how can i write a one line code which chceks the presence of the file? and second line line should take the status of the prevous line. like below. Line 1: checks for the... (16 Replies)
Discussion started by: JSKOBS
16 Replies

5. UNIX for Dummies Questions & Answers

help in finishing 2 commands

the first one: 1. i am trying to build a command that searches a text file and outputs the number of words which consists of two 'b' characters in each word separatly like barby (the b characters must be separated from one another) i tried to use grep filepath then i know that we need... (3 Replies)
Discussion started by: newby2
3 Replies

6. UNIX for Dummies Questions & Answers

scripting issues...

Hi guys, I am thanks in advance for any posts and for reading. I am trying to run this script, under solaris: echo "****************************************" echo "The host is: " echo "****************************************" <<EOF find /etc/default -name login -exec ls -l {} \; find... (5 Replies)
Discussion started by: B14speedfreak
5 Replies

7. Shell Programming and Scripting

Shell scripting issues

hi, I am stuck in a shell script where in i need to extract the specific users from \etc\passwd file. Once this is done, i need to assign Roles to these users which are different for different users. What could be the optimum solution. If i hard code the Roles values in a text file, how would i... (1 Reply)
Discussion started by: ashutosh101
1 Replies

8. Shell Programming and Scripting

Scripting problems

Hello, Im trying to write a script where it will only execute on a certain day. What would the script look like? if then do this is this correct?? (13 Replies)
Discussion started by: lewisoco
13 Replies
Login or Register to Ask a Question