LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-23-2013, 06:06 AM   #1
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Rep: Reputation: Disabled
Question Bash shell scripting basic question regarding the syntax


Consider the script below:

myname=`basename $0`;
for i in `ls -A`
do
if [ $i = $myname ]
then
echo "Sorry i won't rename myself"
else
newname=`echo $i |tr a-z A-Z`
mv $i $newname
fi
done

1) I am aware that basename $0 signifies my script name here. But how? the syntax explanation please. What does $0 mean?

2) In what cases ";" is used at the end of statement in a script? For example, the first line of script ends with ; , whereas the 8th line doesn't. Also, i found that adding/removing semi-colons at the end of some lines (for example: 1st/6th/8th lines) doesn't really have any significance and script runs fine with or without it.
 
Old 09-23-2013, 06:25 AM   #2
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Original Poster
Rep: Reputation: Disabled
Talking

What does $0 mean?

From man bash, section PARAMETERS, subsection Special Parameters:

0 Expands to the name of the shell or shell script. This is set
at shell initialization. If bash is invoked with a file of com‐
mands, $0 is set to the name of that file. If bash is started
with the -c option, then $0 is set to the first argument after
the string to be executed, if one is present. Otherwise, it is
set to the file name used to invoke bash, as given by argument
zero.

It says 0 there, but $0 is meant because $ identifies a parameter.

You can find such information in man pages easily by using the search key /. Just type /\$0 followed by return. The $ has to be quoted as \$ in this case because the $ has a special meaning when searching, and the backslash is needed to "escape" this special meaning.

In what cases ";" is used at the end of statement in a script?

Usually only when you want to put at least two statements in a single line, for example this would be a case where you can use ;:

if [ $i = $myname ]; then

There's no case in your example script where the ; is required, you can remove it from the first line. Details can again be found in man bash:

Lists
A list is a sequence of one or more pipelines separated by one of the
operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
<newline>.

[...]

A sequence of one or more newlines may appear in a list instead of a
semicolon to delimit commands.

If a command is terminated by the control operator &, the shell exe‐
cutes the command in the background in a subshell. The shell does not
wait for the command to finish, and the return status is 0. Commands
separated by a ; are executed sequentially; the shell waits for each
command to terminate in turn. The return status is the exit status of
the last command executed.
 
1 members found this post helpful.
Old 09-23-2013, 10:39 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
BeingGokul

gave ya a thumbs up for a great reply. for future use, please use quote flags as that would make it even easier to follow your detailed post.

good work, keep it up.
 
  


Reply

Tags
basename, bash scripting, shell scripting



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] (I imagine) very very basic question about bash shell scripting fkpacini Programming 13 04-18-2010 11:13 PM
Bash shell scripting question. $Linuxnoob Fedora 13 04-01-2006 01:12 PM
BASH Shell Scripting Question xianzai Programming 5 03-19-2004 07:50 AM
Basic BASH scripting question Dark_Helmet Programming 3 06-26-2003 05:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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