LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-19-2010, 11:57 PM   #1
LostChild1
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
Beginning Bash Scripting Help


Hi again,

I'd like to advise that I am working on a homework project, thus I wouldn't exactly just like the answer, but instead to understand my commands and how to make this work.

Having said that I have this problem and a few potential solutions below:

Code:
1.a) Create a script which will read the names of the directories and  links from the file etcdl3.txt and  
- if the name is a valid directory name  print the message 
File “name” exist and  is a directory
 - if the name is a valid link print the message
File “name” exist and  is a link
- Otherwise it should print the message
File “name” is neither a link nor a directory
Capture the output of this script in file etcdl4a.txt
Where the etcdl3.txt file looks something like this (But with more lines):

Code:
DIRdrwxr-xr-x           reader.conf.d
DIRdrwxr-xr-x           redhat-lsb
DIRdrwxr-xr-x           rhgb
LNKlrwxrwxrwx           rmt
LNKlrwxrwxrwx           rndc.key
DIRdrwxr-xr-x           rpm
DIRdrwxr-xr-x           rwtab.d
DIRdrwxr-xr-x           samba
From the get-go I have a few questions that I can't seem to answer for myself. My teacher isn't the best at explaining things and I'm a little bit lost. In class, when we write our BASH scripts, he is able to do something similar to:

Code:
read X < etcdl3.txt
Which I guess runs through each line of text in the file. Correct? But how would I get a loop that will run through each line in the file until the end? "for X in..."? I'm familiar with the syntax in Java, but not so much in BASH.

Following that, for the if statements I would need to have (To determine if it's a link, directory, or neither) I would assume it would be akin to this:

Code:
if (something -eq "D")
  echo "`cut -d'\t\t' -f2` is a directory"
elif (something -eq "L"
  echo "`cut -d'\t\t' -f2` is a link"
else 
  echo "`cut -d\t\t' -f2` is neither a directory nor a link."
So I have two further questions:

1. Where I wrote "something", how would I isolate the first letter in each line? (as that's all I really need to determine what kind of file it is) Would a `grep '^d'` work in this situation?

2. In echo, I am allowed to back-tick in a cut command correct? Otherwise, how would I be able to get the name of the file? (I figured this would work as it's delimited by two tabs, and is the second field.

Thank you so much for your help.
 
Old 03-20-2010, 12:50 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by LostChild1 View Post
Code:
read X < etcdl3.txt
Which I guess runs through each line of text in the file. Correct? But how would I get a loop that will run through each line in the file until the end? "for X in..."?
No, it will read a single line from etcdl3.txt into $X.
Code:
while read X 
do
    <command(s)>
done < etcdl3.txt
Quote:
Originally Posted by LostChild1 View Post
1. Where I wrote "something", how would I isolate the first letter in each line? (as that's all I really need to determine what kind of file it is) Would a `grep '^d'` work in this situation?

2. In echo, I am allowed to back-tick in a cut command correct? Otherwise, how would I be able to get the name of the file? (I figured this would work as it's delimited by two tabs, and is the second field.
1. The easiest way is to use Shell Parameter Expansion. If the line is in $X the the first character is ${X:0:1}

2. It's not so much "in echo" as "in a double quoted string" and the answer is yes:
Code:
c@CW8:~$ echo "X`echo Y`Z"
XYZ
But $( ) is better practice than ` ` for reasons explained here

BTW:
  • You might prefer a case statement over if-elif-else.
  • It's not if ( <expression> ) but if [[ <expression> ]]. You could use if [ <expression> ] but [[ ]] is better practice for reasons explained here.

Last edited by catkin; 03-20-2010 at 12:51 AM. Reason: Errant left bracket
 
Old 03-20-2010, 11:59 AM   #3
LostChild1
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks so so much. I read your links and it helped a lot, I even (moderately) understand what I'm doing now. And thanks for the heads up on using $() instead of backticks. My teacher uses Backticks so I thought that was the only way, and I thought ${} was the only way to use a dollar-sign (In that case, assigning a variable). But now I see that's not the case at all

However, despite reading part of your Shell Parameter Expansion link, I don't quite understand the PARTS of:

${X:0:1}

If X was a single line, I guess 0 means that it refers to the whole line (Much like a $0?) and the 1 refers to the first letter. Is this correct? How would I use this to say, find the last word's first letter in a line? Or is this not possible / easy with this format?

In any case, I did finish my homework, and I'm quite proud of myself, so a big thank you for helping me out on my syntax and logic, and in the end I did end up going with the case statements. Thanks again!
 
Old 03-20-2010, 12:31 PM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,892
Blog Entries: 27

Rep: Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182Reputation: 2182
Hi,

Welcome to LQ!

Thanks for being upfront that this is for homework.
Look at 'Geek Stuff' to get some good examples and information.

Just a few more links to aid you to gaining some understanding;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just Slackware® links!
 
Old 03-20-2010, 01:08 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by LostChild1 View Post
And thanks for the heads up on using $() instead of backticks. My teacher uses Backticks so I thought that was the only way, and I thought ${} was the only way to use a dollar-sign (In that case, assigning a variable). But now I see that's not the case at all

However, despite reading part of your Shell Parameter Expansion link, I don't quite understand the PARTS of:

${X:0:1}

If X was a single line, I guess 0 means that it refers to the whole line (Much like a $0?) and the 1 refers to the first letter. Is this correct? How would I use this to say, find the last word's first letter in a line? Or is this not possible / easy with this format?
Sometimes being a student is as much about learning inter-personal skills as much as it is about learning technical skills. Historically we only had bacticks so, of course, that was the only way of doing things but backticks were had some shortcomings so later shells evolved. The developers of later shells had a problem of how to change command substitution (= replacing it with the output of the command) without breaking old shell scripts. They came up with the solution of introducing a new format, $( ), so the old shell scripts would still work as they always did and the new format could be used for an improved way of doing things. Unfortunately many teachers didn't keep up with evolution and still teach backticks. OK, for simple commands, backticks mean one less character to type so OK to use for quick-and-dirty at the command prompt but not the most robust choice in scripts. Now you get to teach your teacher and that's where the social skills come in: if they are a good teacher they will be happy to learn something new; if not ...

Here on LQ we see too many variables posted by students as ${foo}. It's never wrong but it is seldom necessary; it is only necessary if the next character after the variable reference could be part of the variable name. For example echo ${foo}bar echoes the contents of $foo concatenated with the string bar whereas echo $foobar echoes the contents of variable $foobar. The {} are also required for array references and for parameter expansion.

Regards ${X:0:1}, $X is simply a variable which may contain anything; line or not doesn't come into it. The 0 means start at the first character of $X and the 1 means get one character from it so ${X:0:1} is the first character of $X. ${X:5:3} would be three characters of $X starting at the sixth.
 
Old 03-20-2010, 04:21 PM   #6
LostChild1
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
I'm sure he'd love to learn it. He's not a bad guy, by any means (And I hope it never came across as such). I'll let him know in the following week for sure.

And yeah, I knew about the ${}, that is one thing he taught us, but that's it's always safe-practice to use ${} just in case. But I see how that would work.

Finally, thanks for the explanation. I appreciate your time and thoughtful replies. Thanks again!
 
  


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
bash scripting ZAMO Linux - General 1 01-19-2009 03:25 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
bash: How to make ^u kill entire line, not just to beginning Yalla-One Slackware 5 04-17-2006 01:19 PM
Handling filename beginning with '--' in bash vjimin Linux - Newbie 2 02-07-2006 05:18 AM
help on bash scripting Kendo1979 Linux - Newbie 3 05-17-2005 08:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration