LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-28-2004, 04:14 PM   #1
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Rep: Reputation: 30
Scripting help


Ok. I am starting to write linux Scipt files.

1. In my own script I didn't put #!/bin/bash or something similar to any line. Still my script worked. Why ?


2. Can I use pipes in scripts like

if [ `ls -al | grep filename` != "" ]; then
echo "file already exists."
exit 127;
fi


3. What scripting language is used with linux ( or with bash actually ). Is it just bash or is bash using perl or something ?

Thanks for your time.
 
Old 07-28-2004, 05:51 PM   #2
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
1) The #! is call the she bang. It tells your shell what program to interpt the rest of the file with. If you don't specify a #! the default is /bin/sh

2) You can use pipes but in this case you should do this ...
Code:
if [ -e $filename ]; then 
...
another way would of been to check the $? after executing a command like this ...
Code:
filename=junk
ls -al $filename > /dev/null
if [ $? == 0 ]
then
        echo "file $filename exist"
fi
no need for grep there.


3) what scripting language is used with linux? too many to list but here's a short list
tcl, bash, csh, sh, perl, python

Last edited by SheldonPlankton; 07-28-2004 at 05:53 PM.
 
Old 07-28-2004, 07:08 PM   #3
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Markelo,
May be the oldest scripting language in *nix is the Bourne shell. Its executable file was "sh". It has some limitations but it was ubiquitous. When computers gained more power, the others started to appear like as "csh" which has a C like syntax, ksh (Korn shell) which maintains the Bourne shell syntax but has new commands and structures and, bash which is an acronym for "Bourne again shell". You can set up your profile to open your console always with one of these. The default for linux is bash. This setup is defined in "/etc/password", where the last field determines the user shell. Perl, python and tcl are powerful scripting languages, but normally not used at the console prompt.
 
Old 07-28-2004, 07:53 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Just to clarify (for clarity's sake), bash uses its own internal language for scripting. It does not (to my knowledge) require perl or any other scripting engine; it is a stand-alone interpreter.
 
Old 07-29-2004, 07:51 AM   #5
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Dark_Helmet
Just to clarify (for clarity's sake), bash uses its own internal language for scripting. It does not (to my knowledge) require perl or any other scripting engine; it is a stand-alone interpreter.
OK. What I meaned was that does it works like a perl interpreter and thus would follow perl grammer or is it different language by itself.

For another matter I would still like to know how I can pipe something to grep witin my scripts. That ls -al | grep filename was just an example.

Ok in truth I was trying to do
lsmod | grep fglrx

You can give help in that also, but please help me with pipe.
 
Old 07-29-2004, 08:12 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
When if-grepping, there's often no need for the '[' and ']'.

Just do:
Code:
if lsmod | grep fglrx >/dev/null ; then
    echo "fglrx module is loaded."
else
    echo "fglrx module is NOT loaded."
fi
"Grep" is the last command in the pipe-line, and as such it provides an exit code for the "if" to use as a condition. When grep finds the string it exits with zero code . If it does not find it, with non-zero exit code. That's all "if" needs.

Last edited by Hko; 07-29-2004 at 08:16 AM.
 
Old 07-29-2004, 11:11 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally posted by markelo
OK. What I meaned was that does it works like a perl interpreter and thus would follow perl grammer or is it different language by itself.
That's what I was trying to say: bash uses an internal, stand-alone interpreter. In that respect, it follows its own grammar, syntax, etc.

Quote:
Originally posted by markelo
For another matter I would still like to know how I can pipe something to grep witin my scripts. That ls -al | grep filename was just an example.
Yes, you can use pipes as much as you like within backticks ( ` ). You can pipe one command into another, or have a string of 5 pipes, 10 pipes, or more.

You can use backticks in if statements like your original post, or you can assign it to a variable:
Code:
if [ "`lsmod | grep fglrx`" != "" ] ; then
...
Code:
fglrx_loaded=`lsmod | grep fglrx`

if[ "$fglrx_loaded" != "" ] ; then
...
The two code snippets above are equivalent. Hko's code is also equivalent. He's directly using the return result of grep, whereas the code above is explicitly testing for grep's failing output (an empty string).
 
  


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
new to scripting mifan Linux - Newbie 2 08-17-2005 12:10 PM
another help with scripting?! ice99 Programming 2 08-09-2005 08:56 AM
Need help scripting Tamara Programming 7 06-06-2005 02:06 AM
Scripting Help Please Jazinator Linux - Newbie 7 10-17-2004 06:35 PM
scripting bforest Linux - Newbie 4 05-11-2004 02:45 AM

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

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