LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 08-23-2005, 10:16 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
Shell Scripting: {..}, ${..}, [[..]], [..], (..), ((..)), $((..))... .my brain hurts!


I've been digging deeper into the shell and shell scripting in last few weeks. Am I the first to notice how *freaking* CONFUSING it gets?!

Especially with all those bracket/parenthesis operators. I mean... IT IS RIDICULOUS!

look at ((...)) for example. IT can be:

assignment operator instead of 'let'
evaluates numeric expressions with $(( ___))
as an arithmetic test inside of [[..]]

and who knows WTF else.

Than another thing:

[..] and [[..]] return 0 for true and 1 for false. Logical operators && and || do it 1 for true and 0 for false! How am I supposed to combine these in a statement like this
Code:
 [[...]] && command
for example?!

Let me ask you experienced guys, but honestly: is it ever gonna clear up in my head? Or am I supposed to be carrying my shell scripting guide to bed with me till I die?
 
Old 08-23-2005, 10:46 PM   #2
PenguinPwrdBox
Member
 
Registered: Oct 2003
Location: /illinois/chicago
Distribution: Slackware/Gentoo/FC/RHEL
Posts: 568

Rep: Reputation: 30
Ok, firsty, stop.
Breathe.
Drink.
Good.
Have you eaten?
Order food.

You are obviously overloaded. So, step away from it, and come back here, into theory for me. It is about the operators, and the precedence....but you will understand that eventually.

You've got the parens down real well. I don't think you need to worry there. You understand what they do. Now, here is the tricky part. You have to understand what to do, with what you know. There are no rules. It's freeform. That's what's cool about programming. Yes, there are right, and wrong ways to do things...but you will learn those as you go on.

It's kinda like playing the guitar. You start out knowing there are six strings, and if you move your fingers, it makes different sounds. But it's what you figure own your own, and what you pick up from people along that way, that give you the ability to play.

Your binary operators for example. You know this:
[[ 3 = 3 ]] will return true, or 0
[[ 3 < 7 ]] will return false, or 1.

So, knowing this, how would you build a construct?
Let's say that you are writing a script. And if your mathematical test is true, you want it to execute a command. So, what do you do?

You want to execute if [[...]] is true which = 0.
You know that || will only execute if the condition before it has a result of 0.

So, you would write the construct:

[[ true ]] || command

And the opposite if you want to run that command if the value is false:

[[ untrue ]] && command

Whatever you do, don't sleep on the book. I tried osmosis. It leads to neckaches

Last edited by PenguinPwrdBox; 08-23-2005 at 10:47 PM.
 
Old 08-23-2005, 11:11 PM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by PenguinPwrdBox
It's kinda like playing the guitar. You start out knowing there are six strings, and if you move your fingers, it makes different sounds. But it's what you figure own your own, and what you pick up from people along that way, that give you the ability to play.

Funny that you mention it, I've been trying to apply the guitar parallel all along. It's one thing that I taught myself how to do (20 years ago), just as I am doing with Linux. But you can't really compare it. Learning to play guitar was a linear thing and this...this is like picking shells at the beach: you never know what you are going to find and where. The worst is that I know I learned a lot, but I am not even close to being even remotely productive.
Plus guitar was mostly about coordination of movement and practice. But this is just so conceptual. Even when I am fairly operative with shell scripting, it's only a prerequisite. I still need to figure out what to do with it, as far as configuring the system and stuff.

I am just in awe of people who know how to work in more than one shell and do perl on top of it, not to mention a serious language like C++ . I mean where do you fit all that stuff?
 
Old 08-23-2005, 11:20 PM   #4
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
Quote:
Ok, firsty, stop.
Yes! If you get frustrated, go take a break. A 15-minute walk around the block allows me to solve more problems then 4 hours at the computer.

Quote:
Originally posted by PenguinPwrdBox

So, you would write the construct:

[[ true ]] || command

And the opposite if you want to run that command if the value is false:

[[ untrue ]] && command
Not to throw gasoline on the fire, but the first statement will never work. The logical or operator '||' will only execute the second statement if the first statement is false.

The second statement will only work if you are testing for the negative of something. So "[[ if not untrue ]] && command" might be a better example (But that is a headache to think about-- not even sure if I got it right

For example, if I have a directory that contains one file named 'foo.txt', the following commands work:

Code:
# '-f foo.txt' is true, therefore 'ls -l foo.txt' is never evaluated because of the ||
rohan:~ % [[ -f foo.txt ]] || ls -l foo.txt
rohan:~ %

# '-f foo.txt' is ture, therefore 'ls -l foo.txt' IS evaluated because of the &&
rohan:~ % [[ -f foo.txt ]] && ls -l foo.txt
-rw-r--r--  1 root root 17969 Aug 23 10:06 foo.txt

# In this next one, '-f bar.txt' returns false, but the not operator '!' says "If this is NOT false", which returns true.
rohan:~ % [[ ! -f bar.txt ]] && ls -l bar.txt
ls: bar.txt: No such file or directory
rohan:~ %

Instead of trying to think in terms of '0' or '1', just try to think in terms of 'true' or 'false'.

Last edited by stefanlasiewski; 08-23-2005 at 11:21 PM.
 
Old 08-24-2005, 04:06 AM   #5
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
I think it will make sense once you find a use for each construct. I'll show two example usages of () and {}.

Everything inside the () is run in a subshell which doesn't affect the current environment.
* You want to cd to a certain directory before wget'ing a file, but you want to stay in the current directory.
* You have to change some important environmental variables for e.g. a loop.
Code:
(
cd downloads/
wget "$url"
)
You can pipe or redirect the output from a subshell, e.g. () | filter or () > file

{} is a block of code, which doesn't run in a subshell.
One usage is a shortand for if-else expressions, e.g:
Code:
true && { echo true; } || { echo false; }
Another usage is to redirect several commands to a file, or to pipe the output (just like with the subshell).
Code:
{
echo "line 1"
echo "line 2"
echo "line 3"
} >test.txt
 
Old 08-24-2005, 07:48 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally posted by frankie_DJ
I am just in awe of people who know how to work in more than one shell and do perl on top of it, not to mention a serious language like C++ . I mean where do you fit all that stuff?
Like everything else, it is just a lot of practice and experience. The longer you stick with it, the deeper your understanding becomes, and things you do everyday become a second nature.

I got into painting miniature figures for wargames and such at a young age. When I first started out, I had one or two really nice figures, but a lot of others were just plain awful looking. As the years went on, I experimented and found things that worked and things that didn't. I got pretty good at it after a while.

Same sort of thing holds with any discipline. I got interested in computers around age 10 when the neighbors first got a Commodore 64. Within a year, we nagged our parents enough to get us one, and I used to stay up at night reading the books that came with it. There was this old game called "Adventure" (and a bunch of these other text adventure games) that I wanted to learn how to make. Now my initial attempts at making games were pretty horrible by any kind of modern programming standard (spaghetti GOTOs like you wouldn't believe!) but I stuck with it, and I got better at it.

The nice thing is that it seems the more you learn about a subject, the easier it get to apply new concepts to it.
 
Old 08-25-2005, 05:33 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
welcome to the wonderful world of
UNIX operating systems

Quote:
[..] and [[..]] return 0 for true and 1 for false. Logical operators && and || do it 1 for true and 0 for false! How am I supposed to combine these in a statement like this

code:

[[...]] && command

for example?!
Er... it works... ?

Code:
$ [ -f ~/.profile ] && echo yes it exists || echo no it don\\'t
yes it exists
$ [ -f ~/.poo-file ] && echo yes it exists || echo no it don\\'t
no it don't
give it a few years you'll do it in your sleep
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting colly Linux - General 3 09-06-2004 05:00 PM
Brain challange : Shell Variable xanthium Programming 5 02-07-2004 11:00 AM
24 hours of kernel compiling. Oh my god my brain hurts.. Pcghost General 16 05-09-2003 06:43 PM


All times are GMT -5. The time now is 03:59 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration