LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-08-2013, 10:15 PM   #1
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Rep: Reputation: Disabled
function


i have downloaded a script file which downloads steam engline, dependicies and then install them. i am trying to understand what code is exactly doing. i have understood some part of it, but could not understand some functions. can someone explain me what these functions doing?

Code:
 download() {
    local url="$1"
    local filename="$(basename "$url")"

    if [ ! -f "$filename" ]; then
        wget -c "$url" -O "$filename.part"
        mv "$filename.part" "$filename"
    fi
}

package() {
    local url="$1"
    local target="$2"

    download "$url"

    mkdir -p "$target"
    ar p "$(basename "$url")" data.tar.gz | tar xz -C "$target"
}

STEAM_DEB_URL="http://media.steampowered.com/client/installer/steam.deb"
UBUNTU_DEB_URL="http://security.ubuntu.com/ubuntu/pool/main/e/eglibc/libc6_2.15-0ubuntu10.2_i386.deb"
 
Old 05-08-2013, 11:42 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Which part you couldn't understand?

Follow this tutorial for better understanding: http://tldp.org/LDP/abs/html/functions.html
 
Old 05-09-2013, 12:00 AM   #3
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by shivaa View Post
Which part you couldn't understand?

Follow this tutorial for better understanding: http://tldp.org/LDP/abs/html/functions.html
i did not understand both download() and package() functions and what these functions will do when they will be called in a script. i do not know what local and basename does. i have gone through the man pages of all the commands but couldn't understand properly what they do. i don't understand what the following is doing

Code:
wget -c "$url" -O "$filename.part"
 
Old 05-09-2013, 12:13 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
basename separates the filename from the dirpath ie
Code:
basename /dir1/dir2/file.ext

# returns
file.ext
http://linux.die.net/man/1/basename which has examples

local makes the var visible only in the fn. By default, vars a re global in shell scripting.

If you don't understand a cmd
1. read the man pages
2. read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
3. google examples
4. Try the cmd out <= this is the important bit there's no substitute for playing around with a cmd/testing it


Apply the above to the wget cmd.

Last edited by chrism01; 05-09-2013 at 12:14 AM.
 
Old 05-09-2013, 02:45 AM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Rep: Reputation: 0
@sam_sung

wget is commonly used on unix/linux servers to download the packages/tools/application software.
Let us examine your function download() as name suggest it is downloading the packages/files from urls.

Code:
 download() {
    local url="$1"
    local filename="$(basename "$url")"

    if [ ! -f "$filename" ]; then
        wget -c "$url" -O "$filename.part"
        mv "$filename.part" "$filename"
    fi
}
download () means defining function download that is how you define in unix.
{, } are like BEGIN and END .
$1 means first command line argument $2 means second command line argument, so on and so forth.

As chrism01 has told local means variable are local within loop and not global.

With ! -f "$filename" means if the *NO* file existing with that package download it with wget command.

It is download and moving it to it's parent directory. Does it make sense now? Run the script with set -xv, it would spit out lot of information.

Hope this helps.
 
Old 05-09-2013, 02:48 AM   #6
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
basename separates the filename from the dirpath ie
Code:
basename /dir1/dir2/file.ext

# returns
file.ext
http://linux.die.net/man/1/basename which has examples

local makes the var visible only in the fn. By default, vars a re global in shell scripting.

If you don't understand a cmd
1. read the man pages
2. read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
3. google examples
4. Try the cmd out <= this is the important bit there's no substitute for playing around with a cmd/testing it


Apply the above to the wget cmd.
thanx chris. i have read the man pages, but i couldn't get everything from it. but your basename explanation was very helpful. but its still not crystal clear, as i did not understand the following part. i understand what wget -c and -O does, but still not clear what it means in this part. is it getting the file in parts and merging the file? and rename the file afterwards?
Code:
 if [ ! -f "$filename" ]; then
        wget -c "$url" -O "$filename.part"
        mv "$filename.part" "$filename"
    fi
 
Old 05-09-2013, 02:55 AM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Rep: Reputation: 0
@sam_sung.

sometimes but not always man page helps.

wget has many switches.

-c switch means continue partially downloaded file
-o means write
-O file
--output-document=file
The documents will not be written to the appropriate files, but all will be concatenated together and writ-
ten to file. If - is used as file, documents will be printed to standard output, disabling link conver-
sion. (Use ./- to print to a file literally named -.)

Note that a combination with -k is only well-defined for downloading a single document.

Somewhere in script that variable "$filename.part" must be defined. Please search and let us know what is it's value.

Thank you
 
Old 05-09-2013, 03:08 AM   #8
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sysmicuser View Post
@sam_sung.

sometimes but not always man page helps.

wget has many switches.

-c switch means continue partially downloaded file
-o means write
-O file
--output-document=file
The documents will not be written to the appropriate files, but all will be concatenated together and writ-
ten to file. If - is used as file, documents will be printed to standard output, disabling link conver-
sion. (Use ./- to print to a file literally named -.)

Note that a combination with -k is only well-defined for downloading a single document.

Somewhere in script that variable "$filename.part" must be defined. Please search and let us know what is it's value.

Thank you
it is defined in the function download() but as filename, but not the filename.part (the script which i have provided in the first post in this thread).
 
  


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
Windows Spawnl (nowait) like function in Linux... How to use Execl function? cheranj Programming 2 09-15-2011 09:01 PM
Calling a function in 1 sec without using any timer based function/API barunparichha Linux - Software 3 09-09-2011 03:15 PM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 11:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 06:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 02:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:07 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration