Non-root script used search and list specific key words


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Non-root script used search and list specific key words
# 1  
Old 06-16-2016
Linux Non-root script used search and list specific key words

Hy there all. Im new here. Olso new to terminal & bash, but it seams that for me it's much easyer to undarsatnd scripts than an actual programming language as c or anyother languare for that matter.

S-o here is one og my home works s-o to speak.

Write a shell script which:
-only works as a non-root user
-gets a directory or tar archive (*.tar.gz, *.tgz) as the main parameter
-traverses all of it and its subdirectories/archives to find all files containing “much Open, such Stack” in them and print their file names (-n parameter must be given for this)
-if tar archives are found, search for files in them as well without leaving any evidence that you unarchived anything
-print the number of found files (-c parameter must be given)
-check all arguments, both -c and -n may or may not have been given

NOTES:
-check all your parameters at the beginning of the script, and save the “presence” of -n and -c in some variables for later use.
The parameters may come in ANY order so do be aware of that.
you can use `$0 param1 param2 …` to make your script “recursive”.


my curent script looks like this:

Code:
!/bin/bash          

echo "Hello, "$USER". This script will search all files containing: much Open, such Stack"
   abc=$1
echo -n "Enter the search path and pres [ENTER]:"
echo

if [ -d $abc ];then
  #find . -type f -print $abc | xargs grep 'much Open, such Stack'
  ls -R $abc | xargs cat | grep 'much Open, such Stack'
else
  if [ "$abc" == *".tar.gz" ]; then
      tar -xvzf $abc
  fi
fi



olso i had one more script done wich did made more sens to me at the time, still dose:

Code:
#!/bin/bash

echo "Hello, "$USER". This script will search all files containing: much Open, such Stack"
$target
echo -n "Enter the search path and pres [ENTER]:"
read target
echo

if [ -d $target ];then
         find . -type f -print $target| xargs grep "much Open, such Stack"
        #ls -F $target| grep -v '/' #| xargs cat | grep "much Open, such Stack"
else
         if [ "$target" == "*.tar.gz" ]; then 
         tar -tzf $target
      fi
fi

as you can se the codes uses one of the to functions "ls" or "find" ( one being disabled )

My question is this:

why dont i get any kind of output on the second code no mater what path i give it, beeing tar, directory or a txt file, and gain no matter what function i use be it find or ls.


and why dose the first code works? sort of...

Any ideas?

Last edited by Crisso2Face; 06-16-2016 at 09:25 AM..
# 2  
Old 06-16-2016
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for specific key in a file and print it

Hi All, I have a file abc.txt Database unique name: NEPAL Database name: NEPAL Services: NEPAL_COB,NEPAL_PROD output i need is it should search "Services" and it that search "COB" word so output i need is like NEPAL_COB (3 Replies)
Discussion started by: amar1208
3 Replies

2. UNIX for Advanced & Expert Users

List all file names that contain two specific words. ( follow up )

Being new to the forum, I tried finding a solution to find files containing 2 words not necessarily on the same line. This thread "List all file names that contain two specific words." answered it in part, but I was looking for a more concise solution. Here's a one-line suggestion... (8 Replies)
Discussion started by: Symbo53
8 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

search from a list of words

Hello, I'm trying to write a bash script that will search for words from one list that may be found in another list. Once the record is found, it will create a new text file for each word. For example, list1.txt contains the following: Dog Cat Fish List2.txt contains Dog - Buddy 14... (3 Replies)
Discussion started by: jl487
3 Replies

5. Shell Programming and Scripting

Search a test file for specific words

I have the need to search a text file from my unix script to determine if it contains the strings of: 'ERROR' and/or 'WARNING'. By using Grep I can search the file and return a where one of these strings exists. Like this: cat myfile.txt | grep ERROR Output: PROCESS ERROR HERE ... (3 Replies)
Discussion started by: buechler66
3 Replies

6. Shell Programming and Scripting

List all file names that contain two specific words.

Hi, all: I would like to search all files under "./" and its subfolders recursively to find out those files contain both word "A" and word "B", and list the filenames finally. How to realize that? Cheers JIA (18 Replies)
Discussion started by: jiapei100
18 Replies

7. Shell Programming and Scripting

how to retrieve the word between 2 specific words with shell script

Hi, I have a file content like: : : <span class="ColorRed"> 1.23</span><br> : : the value 1.23 will be changed from time to time, and I want to use a shell script command, e.g grep or sed, to retrieve only the value, how to do it? Thanks! Victor (6 Replies)
Discussion started by: victorcheung
6 Replies

8. Shell Programming and Scripting

Help adding a key word search to my script

Hello: I need help adding a key word search to my bash script. I have the following script. My boss whats the user to be able to add a search word e.g. unknown failures for the script to search the logs through and find the instances. I had originally done it so it grepped for unknown... (8 Replies)
Discussion started by: taekwondo
8 Replies

9. UNIX for Dummies Questions & Answers

Search File for Specific Words

I have a file that contains the following: Mon Dec 3 15:52:57 PST 2o007: FAILED TO PROCESSED FILE 200712030790881200.TXT - exit code=107 Tue Dec 4 09:08:57 PST 2007: FAILED TO PROCESSED FILE 200712030790879200a.TXT - exit code=107 This file also has a lot more stuff since it is a log file.... (2 Replies)
Discussion started by: mevasquez
2 Replies

10. Shell Programming and Scripting

Search files that all contain 4 specific words

Hello, I want an one line command that brings me back all the files in a folder that contain 4 specific words anywhere inside them. I want to use find,xargs and grep. for example i know for one word the command would be: find . | xargs grep 'Word1' But i don't know for 4 specific words... (13 Replies)
Discussion started by: WoodenSword
13 Replies
Login or Register to Ask a Question