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 07-21-2013, 02:10 PM   #1
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Rep: Reputation: Disabled
Question Passing values to a parameter in script


Hi All,

Following is the script to copy all libraries of a certain command to make my chroot environment.

For example:

./cplb.sh /bin/ls will copy all the libraries of ls command (i.e., the files listed by "ldd /bin/ls" command )

cplb.sh

CHROOT='/var/chroot'
mkdir $CHROOT

for i in $( ldd $* |cut -d " " -f3 |sort |uniq)
do
cp --parents $i $CHROOT
done

echo "Chroot jail is ready. To access it execute: chroot $CHROOT"


Now, when i run ./cplb.sh /bin/ls it works like a charm. But when i run it for multiple commands at the same time, for example..

./cplb.sh /bin/{ls,cat}

it throws an error saying

cannot stat : '/bin/ls:' file or dir not found
cannot stat : '/bin/cat:' file or dir not found

I can see a colon after the command, i.e., /bin/ls: and that colon causes the error. Now, where does it come from and how to resolve this, so that I can run the script for multiple commands at the same time.

Thanks in advance.

Cheers,
Gokul

Linux poses a real challenge for those with a taste for late-night hacking (and/or conversations with God)
 
Old 07-21-2013, 04:54 PM   #2
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thumbs up

This is what the output of ldd /bin/ls /bin/cat might look like:

/bin/ls:
linux-vdso.so.1 (0x00007fffd25df000)
libcap.so.2 => /usr/lib/libcap.so.2 (0x00007f28f8d09000)
libacl.so.1 => /usr/lib/libacl.so.1 (0x00007f28f8b00000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f28f8753000)
libattr.so.1 => /usr/lib/libattr.so.1 (0x00007f28f854e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f28f8f0d000)
/bin/cat:
linux-vdso.so.1 (0x00007ffff9dfe000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fc44208a000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc442437000)

Tried the following instead:

ldd "$@" | cut -d' ' -f3 | sort -u | grep -v ':$' |
while read i
do
cp --parents $i $CHROOT
done

Above, we use grep to remove the /bin/ls: and /bin/cat: lines.

The piping-to-loop style is a bit more idiomatic. sort usually has a -u option to remove duplicates. It's generally preferable to use "$@" instead of $* as that will expand to filenames with spaces and special characters correctly.
 
Old 07-21-2013, 04:56 PM   #3
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Original Poster
Rep: Reputation: Disabled
Would be great if someone could help me with the following question.....

The script copies the libraries alone, whereas it doesn't copies the command itself. I mean, if i run the following command..
./cplb.sh /bin/ls -- here it copies the libraries of ls, whereas it doesn't copy the command /bin/ls. I tried adding few lines to the actual script, the output of which is not as expected. Can you please tell me what should be added to the existing script so that it copies the command also. Thanks in advance.
 
Old 07-21-2013, 05:39 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Probably just add
Code:
cp --parents --target-directory="$CHROOT" "$@"
PS please use [CODE][/CODE] around code.
 
Old 07-22-2013, 12:39 PM   #5
BeingGokul
LQ Newbie
 
Registered: Jul 2013
Location: Bangalore, India
Distribution: RedHat, CentOS
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by ntubski View Post
Probably just add
Code:
cp --parents --target-directory="$CHROOT" "$@"
PS please use [CODE][/CODE] around code.
Finally i got that right. Thanks.

ldd "$@" | cut -d' ' -f3 | sort -u | grep -v ':$' |
while read i
do
cp --parents "$i" "$CHROOT"
done
cp --parents "$@" "$CHROOT"
 
  


Reply



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
passing values to the shell script maooah Programming 5 09-21-2011 08:20 PM
Bash Script: Passing values from script to a c++ executable Timballisto Programming 3 03-09-2011 04:16 AM
want to replace $ with \$ in the passing parameter siva512 Linux - Newbie 1 04-03-2008 12:24 PM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM
parameter passing mechanisms dhanakom Programming 2 09-01-2003 04:15 PM

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

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