Check assign


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check assign
# 1  
Old 09-22-2016
Check assign

I have a person script which has a following statement.

Code:
BUILD_FOLDER=$2
i=$((${#BUILD_FOLDER}-1))

if [ "${BUILD_FOLDER:$i:1}" != "/" ]
then
     BUILD_FOLDER=$BUILD_FOLDER/
     #echo $BUILD_FOLDER

else
     echo "  "
     #echo $BUILD_FOLDER
fi

What and how this statement works ?
Code:
i=$((${#BUILD_FOLDER}-1))


Last edited by Scrutinizer; 09-22-2016 at 04:09 PM.. Reason: code tags
# 2  
Old 09-22-2016
It means assign the length of the variable BUILD_FOLDER minus 1 to variable i

For example:
Code:
$ BUILD_FOLDER=foobarbaz             # the length of the variable is 9
$ echo $((${#BUILD_FOLDER}-1))
8

# 3  
Old 09-22-2016
Hi,

$# Number of arguments supplied. Here ${#BUILD_FOLDER} gives length of the fields given as a input in $2

(( )) form is for arithmetic expansion.

For ex. (( i++ ))

I will try with example as follows:

Code:
$ cat 3.sh 
#!/bin/bash

BUILD_FOLDER=$2
i=$((${#BUILD_FOLDER}-1))
j=0 # Not required 
echo $i
((  j++ ))
echo "j is: "$j

Code:
$ /3.sh hi "how are you"
10
j is: 1
$ ./3.sh hi how are you
2
j is: 1


Last edited by greet_sed; 09-22-2016 at 04:14 PM.. Reason: Updated the description with correct term
# 4  
Old 09-22-2016
Code:
i=$((${#BUILD_FOLDER}-1))

${#BUILD_FOLDER} is the length (number of characters) of the variable BUILD_FOLDER

$(( xxx -1)) is xxx minus one

The resulting value is affected to i which then contains the offset of the last character of the string $BUILD_FOLDER
# 5  
Old 09-22-2016
If you don't really need to sometimes print a line just containing two spaces, you could replace everything you showed us in post #1 with just:
Code:
BUILD_FOLDER=${2%/}/

which strips off a trailing slash from the contents of $2 if and only if there was one there, and then adds a trailing slash.
# 6  
Old 09-22-2016
Thank you so much Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check unused ports in a given range and assign an open one

Hi. I need to add code to my KSH script to automatically assign an open port number from a pre-defined range to an Oracle listener. Should I use: lsof -i or netstat -vatn or something else? Thanks. (9 Replies)
Discussion started by: user052009
9 Replies

2. Shell Programming and Scripting

Unable to assign a value

I have written a shell script to calculate dbsize :- db2 "call get_dbsize_info(?,?,?,-1)" | sed -n '8p' | awk -F : '{print $2}' dbsize=`db2 "call get_dbsize_info(?,?,?,-1)" | sed -n '8p' | awk -F : '{print $2}'` echo $dbsize when I execute it the syntax works but it's not... (11 Replies)
Discussion started by: lazydev
11 Replies

3. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

4. Linux

HOW TO assign IP in REDHAT 6?

Hi Guys, It may sound silly questions but I am really confused and forgot the sequence to set IP in REDHAT 6. 1st type system-config-network then I give IP as 192.168.1.78 Subnet as 255.255.255.0 Then do /etc/init.d/network restart Then when I check with ifconfig eth0... (1 Reply)
Discussion started by: manalisharmabe
1 Replies

5. Shell Programming and Scripting

how to assign value

#! /bin/bash if ; then echo "Set number " else k=$1 sqlplus ${scheme}/${apsswd}@${server} @query.sql $k fi file query.sql looks like this select * from tab1 where number =${k}; =================================== it doesnt work my question is how to assign k value in last... (2 Replies)
Discussion started by: kvok
2 Replies

6. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

7. Shell Programming and Scripting

Assign the value

DATA --------------- 0 Please tell me, if the file contains 0 after --. then assign the value to variable $var=false, DATA --------------- 1 then $var=true, (2 Replies)
Discussion started by: sandy1028
2 Replies

8. Shell Programming and Scripting

Using bc to assign value

Friends here is code which is used to add floating point using bc, but I m not getting any output instead some errors. 1 #!/bin/bash 4 if 5 then 6 echo "Our input is from a Device" 7 while read myline 8 do 9 10 total= `echo $total + $myline... (2 Replies)
Discussion started by: navjinder
2 Replies

9. Shell Programming and Scripting

how to check a word in a file and assign to a variable

Hi; I'm a newbie in UNIX, would appreciate for the help. I have a unix oracle script (lets call it orcl.sh) The scripts does a login via sqlplus and does some DDL. The orcl.sh script spool the output of the DDL into orcl.log The orcl.sh is executed via another script call A.sh. It is call... (1 Reply)
Discussion started by: hippo2020
1 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question