LinuxQuestions.org
Help answer threads with 0 replies.
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 11-08-2010, 02:27 PM   #1
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
Reading From Text File In Bash


Hello everyone.. i am kinda new in bash language , well i did some good stuff but still am a beginner so Here is what i cant figure out
I need to Read a path of a file witch is written in Text file
i used this
Code:
FILENAME=$1
while read line
do
echo $line
done < $FILENAME
it worked and showed me the Line witch was written in my file
but now my problem is how am gonna use that line as a path
i mean for example if am gonna execute a linux command on that file
like dpkg -i /path/to/the/file how am gonna export it from The $Line variable and use it after the command
i know it may be easy for most of you but as i said am only a beginner
i hope i can find help. Thank's
 
Old 11-08-2010, 02:36 PM   #2
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Rep: Reputation: 1
You could store $line in an array. This document would be useful.

If it's only one line that is the path, just assign it to a variable called, for example, path--while read line; do path=$line; done < $FILENAME

I have a nagging feeling there is some better way to do it in the second case, but I can't think of it.

Last edited by acvoight; 11-08-2010 at 02:44 PM. Reason: Was terribly unclear in the one line example
 
Old 11-08-2010, 02:46 PM   #3
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
so you mean if it's an only line i can just type dpkg -i $line ??
 
Old 11-08-2010, 02:58 PM   #4
acvoight
LQ Newbie
 
Registered: Jul 2010
Distribution: Linux Mint
Posts: 21

Rep: Reputation: 1
Yes, you could put dpkg -i $line in the while..do..done loop (instead of echo $line), as long as that loop will only read paths that you want to use dpkg -i $line on. (There's that simple solution I had the nagging feeling about!)
 
Old 11-08-2010, 03:05 PM   #5
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
ok i did what u said then i did sudo ./file.sh file.txt but it shows me this error ./file.sh: line 17: syntax error: unexpected end of file
here is what i did with The code

FILENAME=$1
while read line
do
dpkg -i $line
done < $FILENAME

Last edited by ke3r4; 11-08-2010 at 03:10 PM.
 
Old 11-08-2010, 03:24 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
My guess at this time is that either $FILENAME or one of $line contains a space or something else pesky, OR there is garbage in the file.txt file. I would edit the code like so:
Code:
FILENAME="$1"
while read line
do
dpkg -i "$line"
done < "$FILENAME"
However if this continues to not work, please post for us the whole script (obviously there are not 17 lines in this code, so you must have more code involved (or garbage in the input file), and that may well be where the problem is). Also in case this still doesn't work, show us the contents of the input file (file.txt).

Last edited by GrapefruiTgirl; 11-08-2010 at 03:36 PM.
 
Old 11-10-2010, 01:22 PM   #7
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
sorry for being a bit late
anyway i tryed everything that's been said in here but nothing realy worked
and for the hole code lol that's the first step in my code there is nothing before it
 
Old 11-10-2010, 01:29 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Well, post us the input data file. (if it is gigantic, you may want to "attach" it rather than post it, but if you post it, please use code tags: http://www.phpbb.com/community/faq.php?mode=bbcode#f2r1 ).

And, please show us what happens when you run your code. I.e. what I mean is, open your terminal, run your code, than copy + paste for us ALL the output, from where you ran your code, right to the end. This will show us the error messages, and maybe help us figure out what's going on.

Also - where is $1 getting set? This code of yours, must be either in a script, or be a shell function, in order for $1 to have any meaning. EDIT - I see it, never mind this part.

Last edited by GrapefruiTgirl; 11-10-2010 at 01:30 PM.
 
Old 11-11-2010, 06:23 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I also like to give some suggestions, just in case they could also help:

(a) verify that both files (script and text) are in UNIX format and that they have no garbage white spaces

(b) run the code with this command instead:

Code:
sudo bash ./file.sh file.txt
 
Old 11-11-2010, 07:40 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ke3r4 View Post
ok i did what u said then i did sudo ./file.sh file.txt but it shows me this error ./file.sh: line 17: syntax error: unexpected end of file
here is what i did with The code

FILENAME=$1
while read line
do
dpkg -i $line
done < $FILENAME
There are not 17 lines in the code you posted! Please post the whole file.sh script.
 
Old 11-11-2010, 01:29 PM   #11
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
PHP Code:
#!/bin/bash
# ke3r4
# Main/Start
FILENAME=$1
while read line
do
dpkg -i $line
done 
$FILENAME 
ok here is my final code now now it just stuck after typing my password
 
Old 11-11-2010, 03:17 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It should be done < $FILENAME but if dpkg needs to read your password from stdin it will not work. You could try dpkg -i $line < /dev/tty and if that doesn't work we'll have to read from $FILENAME on another file descriptor so ask again.
 
Old 11-12-2010, 01:32 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@ke3r4: You should have at least followed GrapefruiTgirl's suggestion in post #6 and #8.
 
Old 11-12-2010, 05:14 AM   #14
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
Even though we're still awaiting further input here, I thought I'd toss this into the mix as an alternative to the above code that we're currently working with:
Code:
#!/bin/bash
xargs --arg-file=file.txt -I{} dpkg -i '{}'
Note that it may not make any difference regarding the ongoing problem(s) with the current code -it's merely a suggestion of an alternate way to accomplish the same thing. It may suffer from the same problems as the current method is experiencing.

Regardless, we still want to see the input file.
 
Old 11-12-2010, 07:07 AM   #15
ke3r4
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Ok it's starting to piss me off
here is my original code
PHP Code:
#!/bin/bash
#ke3r4
#HybridInstaller
echo "HybridInstaller by ke3r4"
echo "checking for super user"
if [ "$(id -u)" != "0" ]; then
   
echo "[-] Not a superuser." 1>&2
   
exit 0
FILENAME
=$1
while read line
do
dpkg -i $line
done 
$FILENAME 
and here is what it shows me after running sudo bash file.sh Text.txt witch in text.tx i just typed a path to a .deb file

Code:
root@ke3r4:~# bash file.sh Text.txt
HybridInstaller by ke3r4
checking for super user
file.sh: line 14: syntax error: unexpected end of file
PS: To make things clear am programming an application with gambas witch helps ubuntu users to install any application easily including packages from other distro or packages with source file that need to be compiled
this application is a GUI app. now all i want to do is to design with gambas and to do what ever the user want the application call a bash script to do it.

Last edited by ke3r4; 11-12-2010 at 07:14 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
reading path from a text file using bash script mksc Linux - Newbie 2 06-27-2008 03:25 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM

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

All times are GMT -5. The time now is 10:57 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