My first script

I'm trying to get this script to work i'm close but there's some wrong any help is welcome. I have a bunch of text files in a directory which are a list of numbers i'm trying to get this script to look for a random number in the list and tell me in which text file that number is.

---------- Post updated at 04:16 PM ---------- Previous update was at 04:15 PM ----------

#looking for a ticket clear echo "Ticket #:" read ticket echo "number entered" "$ticket" echo grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

Is this homework?

I don't think pasting your code worked. Try pasting it again in code tags

When posting code use

```text
 and 
```

tags around your code eg:

#looking for a ticket clear
echo "Ticket #:"
read ticket
echo "number entered" "$ticket"
echo
grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

What is the problem? This appears like it should do what you describe.

Nope i'm justrying to learn some sripting and this the first one i can use myself this is the code : #looking for a ticket clear echo "Ticket #:" read ticket echo "number entered" "$ticket" echo grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

Soething has gone wrong with your post. The code posted appears to contain html characters and some extra punctuation.

Did you mean:

#looking for a ticket
clear
echo "Ticket #: "
read ticket
echo "number entered: $ticket"
grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

Ps. In removing various &quot and semi-colons and also inserting missing line-breaks, I may have fixed the syntax problems by accident.

@Chubler-XL. Posts crossed. Great minds think alike. Comparing our scripts, you lost a "clear" and I lost an "echo" !

@shadow.wodahs . Line breaks, character spacing and character sets are critical to successful scripting. A semi-colon can be used as a substitute for a line break, but it often makes scripts unreadable.

yes i did. Well it gives me the list of all text files in the folder but i just need the one name of the text file where ticket number i'm looking for is. so say i type 7932 and this number is in file.txt but insted of just getting that i get the list of all text files file.txt,file2.txt,file3.txt etc..

I doubt that grep is lying to you, maybe the ticket number -- or numbers containing that ticket number -- do exist in all those files.

Show some sample data from those files so we can help narrow it down.

Ticket #: 7932 /home/ubu/Desktop/jobs/Holdup10.txt /home/ubu/Desktop/jobs/Holdup11.txt /home/ubu/Desktop/jobs/Holdup12.txt /home/ubu/Desktop/jobs/Holdup13.txt /home/ubu/Desktop/jobs/Holdup14.txt /home/ubu/Desktop/jobs/Holdup1.txt /home/ubu/Desktop/jobs/Holdup2.txt /home/ubu/Desktop/jobs/Holdup3.txt /home/ubu/Desktop/jobs/Holdup4.txt /home/ubu/Desktop/jobs/Holdup5.txt /home/ubu/Desktop/jobs/Holdup6.txt /home/ubu/Desktop/jobs/Holdup7.txt /home/ubu/Desktop/jobs/Holdup9.txt ubu@ubu-desktop:~/Desktop/jobs$ grep -l "7932" *.txt Holdup3.txt ubu@ubu-desktop:~/Desktop/jobs$

If for testing purposes you change grep -l to just grep it will show you the lines in the files which contain the value you typed (which was stored in $ticket).

Slightly off topic. Are you having difficulty cutting text from your screen into posts on unix.com. We see unformatted script with no line breaks including various &quot strings in place of real double quote characters. Do you see the same effect in your browser?

How exactly are you pasting this? What web browser are you using?

I did changed it to data it didn't make a diference

How exactly are you pasting this? What web browser are you using? We can't really tell what you're doing since it gets turned into garbage when you paste it.

Ticket #:
7932
/home/ubu/Desktop/jobs/Holdup10.txt
/home/ubu/Desktop/jobs/Holdup11.txt
/home/ubu/Desktop/jobs/Holdup12.txt
/home/ubu/Desktop/jobs/Holdup13.txt
/home/ubu/Desktop/jobs/Holdup14.txt
/home/ubu/Desktop/jobs/Holdup1.txt
/home/ubu/Desktop/jobs/Holdup2.txt
/home/ubu/Desktop/jobs/Holdup3.txt
/home/ubu/Desktop/jobs/Holdup4.txt
/home/ubu/Desktop/jobs/Holdup5.txt
/home/ubu/Desktop/jobs/Holdup6.txt
/home/ubu/Desktop/jobs/Holdup7.txt
/home/ubu/Desktop/jobs/Holdup9.txt
ubu@ubu-desktop:~/Desktop/jobs$ grep -l "7932" *.txt
Holdup3.txt
ubu@ubu-desktop:~/Desktop/jobs$

---------- Post updated at 04:48 PM ---------- Previous update was at 04:47 PM ----------

sorry java wasn't enable in my browser Noscript was enable

Now, please re-post the script. It's behaving as if $ticket is blank for some reason. I note that the confirmatory echo of the number entered is missing from the above post.
Please also show how you are running this script.

Ps. Corona688 posted a link earlier in this thread about how to use code tags when posting code or data.

#looking for a ticket 
clear
echo "Ticket #:"
read ticket
echo "number entered" "$ticket"
echo grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

Strange the output I would have expected from the above script is:

echo "Ticket #:
7932
number entered 7932
grep -l 7932 /home/ubu/Desktop/jobs/Holdup10.txt /home/ubu/Desktop/jobs/Holdup11.txt ....

Which still dosn't match what you posted as the output earlier.

Anyway remove the echo from the last line of the script ie:
grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt
not
echo grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt

1 Like

Ref post #14
Please also show how you are running this script.

We would expect something like:

chmod 755 scriptname.sh  # One-time change to make the script executable
./scriptname.sh          # Execute the script

I remove echo like Chubler_XL said same output is given.

grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt
Ticket #:
7932
/home/ubu/Desktop/jobs/Holdup10.txt
/home/ubu/Desktop/jobs/Holdup11.txt
/home/ubu/Desktop/jobs/Holdup12.txt
/home/ubu/Desktop/jobs/Holdup13.txt
/home/ubu/Desktop/jobs/Holdup14.txt
/home/ubu/Desktop/jobs/Holdup1.txt
/home/ubu/Desktop/jobs/Holdup2.txt
/home/ubu/Desktop/jobs/Holdup3.txt
/home/ubu/Desktop/jobs/Holdup4.txt
/home/ubu/Desktop/jobs/Holdup5.txt
/home/ubu/Desktop/jobs/Holdup6.txt
/home/ubu/Desktop/jobs/Holdup7.txt
/home/ubu/Desktop/jobs/Holdup9.txt

Hmm: In all your posts this line never seems to execute.

echo "number entered" "$ticket"

Please post the script you actually ran and exactly what you typed to invoke the script. If you just did cut/paste to the command line, please tell us because it will cause the commands to behave differently from when they are executed as a properly formatted Shell Script.

The way i run this is from /home/ubu/Desktop/jobs,
./ticket.sh is exec, permissions chmod 777 as a root
thanks for the Help

#looking for a ticket 
clear
echo "Ticket #:"
read $ticket
grep -l "$ticket" /home/ubu/Desktop/jobs/*.txt