LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2010, 11:38 AM   #1
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Rep: Reputation: 16
Opening a file


I am having trouble opening a file and reading its output.

This is what I have so far:

Code:
#!/bin/bash
echo "Please type a file name"
read fname
cat $fname
What am I doing wrong? When I run this nothing happens.

Tarken
 
Old 02-24-2010, 11:46 AM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
The shell script is ok. If you change "cat $fname" to "echo fname=$fname", what will you see?

Your filename can contain white space, it's better to use
Code:
cat "$fname"
instead.

Last edited by irmin; 02-24-2010 at 11:48 AM.
 
Old 02-24-2010, 11:51 AM   #3
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
That worked thanks!
 
Old 02-24-2010, 11:54 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Works here...

Are you sure that the file exists in the current working directory (from where you are running the script)

Also try:
Code:
#!/bin/bash
read -p "please type filename:  " fname
cat $fname

Last edited by pixellany; 02-24-2010 at 11:55 AM.
 
Old 02-24-2010, 11:54 AM   #5
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
I am still having a problem with a for loop.

Code:
#!/bin/bash
echo "Please type a file name"
read fname
cat $fname           

    for i in $fname 

do
  nslookup  $i
When I run this, it tries to do a nslookup for the file I typed instead of the files output. How do I remedy this?

Tarken
 
Old 02-24-2010, 11:56 AM   #6
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by pixellany View Post
Works here...

Are you sure that the file exists in the current working directory (from where you are running the script)

Also try:
Code:
#!/bin/bash
read -p "please type filename:  " fname
cat $fname
Yeah, it was my fault. I realized that the file existed, but had no data in it. :P
 
Old 02-24-2010, 12:06 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
first, note the format of the "for" loop:
Code:
for <expression>; do
   <do stuff>
   <do more stuff>
done
But;
I would do it this way:
Code:
while read name; do
   <stuff>
done < filename
This reads from filename, one line at a time, and puts the line into the variable "name"
 
Old 02-24-2010, 12:14 PM   #8
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Thank you for your time!

I tried to do that, but now I am getting different errors

Here is what I have:
Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

  nslookup
  ping -c 5

done
What am I doing wrong?

Tarken
 
Old 02-24-2010, 12:27 PM   #9
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Quote:
Originally Posted by tarken View Post
Thank you for your time!

I tried to do that, but now I am getting different errors

Here is what I have:
Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

  nslookup
  ping -c 5

done
What am I doing wrong?

Tarken
Do you mean:
Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

  nslookup $name
  ping -c 5 $name

done < $fname
 
1 members found this post helpful.
Old 02-24-2010, 12:44 PM   #10
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Smile

Thats what I mean! So awesome, thank you very much! One last detail. How do I get it to do all of my commands on each domain name before it moves to the next one. Currently it is doing nslookup on all of them, then pinging all of them.

Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

  nslookup $name
  ping -c 5 $name
  traceroute -w 2 -m 20 $name
  if  whois $name | egrep '\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}'; then
        null
else
echo "No email"
echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fi

done < $fname
 
Old 02-24-2010, 12:50 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What is the content of the variable "name" each time thru the loop? (Insert an echo command to find out.) It sounds like you are getting multiple items on one line.
 
Old 02-24-2010, 01:24 PM   #12
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Quote:
Originally Posted by tarken View Post
Thats what I mean! So awesome, thank you very much! One last detail. How do I get it to do all of my commands on each domain name before it moves to the next one. Currently it is doing nslookup on all of them, then pinging all of them.

Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

  nslookup $name
  ping -c 5 $name
  traceroute -w 2 -m 20 $name
  if  whois $name | egrep '\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}'; then
        null
else
echo "No email"
echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fi

done < $fname
You can do a while loop on the output of the commands too.
Code:
...

nslookup $domain | awk '/^Address:/ { if(ok) { print $2;} } /answer/ { ok=1; }' | while read address; do
ping -c 5 $address
done

whois $domain | egrep '\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}' | awk '{print $2;}' | while read email; do
 echo EMAIL: $email
done

...
 
Old 02-24-2010, 03:01 PM   #13
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
I keep getting a syntax error on line 16, saying an unexpected EOF. There is nothing on line 16.


Code:
#!/bin/bash
echo "Please type a file name"
read fname
while read name; do

nslookup $domain | awk '/^Address:/ { if(ok) { print $2;} } /answer/ { ok=1; }' | while read address; do
ping -c 5 $address
whois $domain | egrep '\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}' | awk '{print $2;}' | while read email; do
 echo EMAIL: $email


done
 
Old 02-24-2010, 03:09 PM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
You have started two loops but only closed one. The script is expecting another "done".

Edit: Actually, you have started three loops. Are you unable to cut and paste from the code that people post for you?

Evo2.

Last edited by evo2; 02-24-2010 at 03:12 PM.
 
Old 02-24-2010, 03:11 PM   #15
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
note: domain = name
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Long file names on Linux file server jumbled when opening on 16-bit PC program. brandonhughesj General 3 03-04-2009 07:53 AM
Help opening file LinuxPimp Linux - General 7 01-26-2007 12:07 AM
Xp user opens file via Samba, but file is not locked after opening??? marisdembovskis Linux - Networking 8 07-04-2006 03:30 AM
Opening a .tar file njschroe Linux - Software 1 02-02-2005 12:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration