LinuxQuestions.org
Help answer threads with 0 replies.
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 04-09-2009, 08:04 AM   #1
jefn
Member
 
Registered: Mar 2009
Posts: 37

Rep: Reputation: 15
Small part of Bash Script that I would like to explain it to me


Hi folks,

I do not have experience with Linux bash scripting and I need to write the script in Java which I have to know the function of every line before starting programming in Java. I have a part of the script that I could not understand:
#-------------------------------------------------------
#!bin/bash
#
HOME=/u/cluster/mike
LOG=$HOME/tmp/fw.log
LOCK=$HOME/tmp/fw.lock

export HOME LOG LOCK

dbg = 0

if [ ${#1} - gt 0 ]; then
[ $1 = "-d" ] && dbg = 1 && echo "Debug mode"


#-------------------------------------------------------

I do not understand :

if [ ${#1} - gt 0 ]; then
[ $1 = "-d" ] && dbg = 1 && echo "Debug mode"


Could you please help me to explain what does it means especially ${#1} and $1 ="-d"

Thanks in advance;
Jefn
 
Old 04-09-2009, 08:33 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Code:
do not understand :

if [ ${#1} - gt 0 ]; then
[ $1 = "-d" ] && dbg = 1 && echo "Debug mode"
I think the first part may be slightly wrong but it'll still work. This ${#} gives the number of arguments passed but ${#1} will work. What this does is check if the number of arguments passed are greater than 0 and if they are checks if the first argument is "-d". If it is then it sets "dbg=1" and echoes "Debug" to the screen.
 
Old 04-09-2009, 09:02 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Slight clarification - ${#1} should give the length of the string in ${1}.

Dave
 
Old 04-09-2009, 09:15 AM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Quote:
Slight clarification - ${#1} should give the length of the string in ${1}.
Shucks - you're right ! Thanks for the pointer. (I should've checked this more thoroughly). The general action of the script is still as I said but thanks again for the correction.
 
Old 04-09-2009, 07:03 PM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Quote:
Originally Posted by jefn View Post
if [ ${#1} - gt 0 ]; then
[ $1 = "-d" ] && dbg = 1 && echo "Debug mode"
What is this, a contest for writing obfuscated bash code?

- g Space allowed between '-' and 'g'?
dbg = 1 &&
Why using the result of 'dbg = 1' to execute the echo command?
Since when spaces are allowed in an assignment?

Maybe the code works, just as with the ${#1} statement, it might work, but it all is very sloppy. Shame on the original programmer.

jlinkels
 
Old 04-10-2009, 01:42 AM   #6
jefn
Member
 
Registered: Mar 2009
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bgeddy View Post
Code:
do not understand :

if [ ${#1} - gt 0 ]; then
[ $1 = "-d" ] && dbg = 1 && echo "Debug mode"
I think the first part may be slightly wrong but it'll still work. This ${#} gives the number of arguments passed but ${#1} will work. What this does is check if the number of arguments passed are greater than 0 and if they are checks if the first argument is "-d". If it is then it sets "dbg=1" and echoes "Debug" to the screen.

You said it is he number of arguments passed. But, I did not set a value above and where is it passed. I posted the top of the code and if you see it is not defined before in the file.


Thanks very much for your help
 
Old 04-10-2009, 01:45 AM   #7
jefn
Member
 
Registered: Mar 2009
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ilikejam View Post
Slight clarification - ${#1} should give the length of the string in ${1}.

Dave

But if see the code ${1} is not defined before, so how can I get the length and it is not defined in the code.

Thanks Dave for the explanation
 
Old 04-10-2009, 01:51 AM   #8
jefn
Member
 
Registered: Mar 2009
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlinkels View Post
What is this, a contest for writing obfuscated bash code?

- g Space allowed between '-' and 'g'?
dbg = 1 &&
Why using the result of 'dbg = 1' to execute the echo command?
Since when spaces are allowed in an assignment?

Maybe the code works, just as with the ${#1} statement, it might work, but it all is very sloppy. Shame on the original programmer.

jlinkels
lol, it is really shame on the original programmer. There were NO comments and the way was very sloppy.


The main purpose of this part of the code is to check if there is wrong with the system. if there , then echo debug and exit.


I could not understand the code, it is really strange and I have NO idea about linux shell scripts. I am mainly in Java
 
Old 04-10-2009, 01:55 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by jefn View Post
But, I did not set a value above and where is it passed. I posted the top of the code and if you see it is not defined before in the file
The argument to a shell script are those passed through the command line. If you execute your script like this:
Code:
./script.sh mickey mouse donald duck
the strings mickey, mouse, donald and duck are 4 arguments passed to the script. Inside the code you can refer to them as $1, $2, $3, $4 respectively. Moreover, as already stated in previous posts, the special variable $# gives the number of arguments, while ${#1} gives the length of the first argument.

If you read a bash programming guide you will find the arguments referred as "positional parameters". Check for them in the Advanced Bash Scripting Guide or any other Bash manual.
 
Old 04-10-2009, 01:59 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by jefn View Post
The main purpose of this part of the code is to check if there is wrong with the system. if there , then echo debug and exit.
Nope. The purpose of this part of code is to check if the user pass the -d option and then run the script in debug mode (without exiting). It is not a smart way to check for options, anyway. If I can give an advice, looking at the amount of syntax errors in few lines of code, I'd throw this script in the trash and eventually try to re-write it from scratch!
 
Old 04-11-2009, 04:09 AM   #11
jefn
Member
 
Registered: Mar 2009
Posts: 37

Original Poster
Rep: Reputation: 15
thanks mate

Quote:
Originally Posted by colucix View Post
Nope. The purpose of this part of code is to check if the user pass the -d option and then run the script in debug mode (without exiting). It is not a smart way to check for options, anyway. If I can give an advice, looking at the amount of syntax errors in few lines of code, I'd throw this script in the trash and eventually try to re-write it from scratch!

lol, you are right. the problem that I have to follow its procedure to program in Java and I don't have Ideas how to use Linux scripting, I have just started to learn it but I have time limits.

Do you have an idea how to get the IP address and certificate information of a certain user request using scripting?. In my case, I would use it to authenticate a Globus (Grids application) user to open ports 2811 and 2119.
 
  


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
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM


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