LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-10-2005, 11:47 AM   #1
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
making a bash script


Here's my situation:

I want to run a script that creates html pages from my man pages. I already have the tool to do that, and know a little bit about scripting, but not that much. I've read through some tutorials already, and it's difficult to find what i'm looking for.

So far I list the contents of three folders and concatenate them into one file, thus giving me one long list of single words (commands, in this case):
Code:
#!/bin/bash
path1=/bin
path2=/usr/bin
path3=/usr/local/bin

filename=allBinaries.txt

ls -1 ${path1} > ${filename}
ls -1 ${path2} >> ${filename}
ls -1 ${path3} >> ${filename}
I would like to read in each line from ${filename} and store each string (each line - each command) into one spot in an array. I can't seem to figure out how to list one line of a file with more, less, tail or cat (but i may have overlooked it). Is this even possible? Or even better, would i be able to pipe the output of ls -l into an array?

Thanks for any feedback.
 
Old 01-10-2005, 01:20 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Download a copy of the "Absolute bash-scripting guide'. Ch. 26 covers arrays, with several examples.
It should be fairly straightforward to declare an array, and use a for-next loop to read from a text file, line-by-line, into an array.
 
Old 01-10-2005, 01:36 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Maybe something like this will get you started
Code:
#!/bin/bash
path="/bin /usr/bin /usr/local/bin"

echo > /home/filename.txt

#save the list
for i in `ls -1 $path` ;do
echo $i >> /home/filename.txt
done

#do something else
file=/home/filename.txt
cat ${file} |\
while read name; do
echo $name
done
 
Old 01-10-2005, 07:13 PM   #4
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Thanks both of you
 
Old 01-10-2005, 11:41 PM   #5
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Though i do have one more question...
Code:
...
# create html files from man pages
line=1
while [ ${line} -le ${number} ]
do  
  if [ -f `man -w ${array[${line}]}` ]
      then
      echo "Creating html" ${array[${line}]}".html"
      ${man} ${array[${line}]} | ${man2html} > ${array[${line}]}".html"
      line=${line}+1
  else
      echo "============================"
      line=${line}+1
  fi
done
When that checks for a man page of "bash2" (which doesn't exist) it still executes as TRUE and makes a man2html html page, but with nothing in it. Why is this not working? How could I make the if statement return FALSE when a man page does not exist?

Thanks
 
Old 01-11-2005, 03:41 AM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Here is a snippet of code I used in a Unix class a couple of years ago. In the script from which this is excerpted, the user supplies two filenames, and this code checks to see if the file exists and whether it's a regular file or a directory.
You could write a bash function to enclude code to test for the existance of a file before writing the html code.

Code:
# check for valid filename entries in this directory  
if [ -e "$1" -a -e "$2" -a -r "$1" -a -d "$2" ]; then
    echo "$1 is a regular file; $2 is a directory."
    echo "Exiting"
    exit 40 
elif [ -e "$1" -a -e "$2" -a -d "$1" -a -r "$2" ]; then
    echo "$1 is a directory; $2 is a regular file."
    echo "Exiting"
    exit 40 
elif [ -e "$1" -a -e "$2" -a ! -d "$1" -a ! -r "$1" -a \
    ! -d "$2" -a ! -r "$2" ]; then
    echo "Neither file is a directory or regular file."
    echo "Exiting"
    exit 30 
elif [ ! -e "$1" -a ! -e "$2" ]; then
    echo "Neither file exists in this directory."
    echo "Exiting"
    exit 30 
elif  [ ! -e "$1" -o ! -e "$2" ]; then
    echo "One of the files doesn't exist in this directory." 
    echo "Exiting"
    exit 30 
else [ -e "$1" -a -e "$2" -a -r "$1" -a -r "$2" ]
    echo "Both files exist in this directory."
    echo "Both files are regular files." 
fi
In bash scripting,
-e = exists
! -e = does not exist
-r = regular file
! -r = nor a regular file
-d = directory
! -d = not a directory
-a = and
-o = or
 
  


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
making a bash script file Berticus Linux - General 1 10-15-2005 11:14 PM
making select show its menu in a bash script? zidane_tribal Programming 6 05-02-2005 05:52 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:15 PM.

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