LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Small part of Bash Script that I would like to explain it to me (https://www.linuxquestions.org/questions/programming-9/small-part-of-bash-script-that-i-would-like-to-explain-it-to-me-717937/)

jefn 04-09-2009 08:04 AM

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

bgeddy 04-09-2009 08:33 AM

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.

ilikejam 04-09-2009 09:02 AM

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

Dave

bgeddy 04-09-2009 09:15 AM

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.

jlinkels 04-09-2009 07:03 PM

Quote:

Originally Posted by jefn (Post 3503488)
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

jefn 04-10-2009 01:42 AM

Quote:

Originally Posted by bgeddy (Post 3503515)
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

jefn 04-10-2009 01:45 AM

Quote:

Originally Posted by ilikejam (Post 3503542)
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 :)

jefn 04-10-2009 01:51 AM

Quote:

Originally Posted by jlinkels (Post 3504128)
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 :(

colucix 04-10-2009 01:55 AM

Quote:

Originally Posted by jefn (Post 3504336)
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.

colucix 04-10-2009 01:59 AM

Quote:

Originally Posted by jefn (Post 3504339)
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!

jefn 04-11-2009 04:09 AM

thanks mate
 
Quote:

Originally Posted by colucix (Post 3504344)
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.


All times are GMT -5. The time now is 07:48 PM.