LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-06-2009, 11:21 PM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
BASH --> Function, while & for loop's sanity check


I am very new with programming... and have been using recommended guides.. very helpful...


I am uncertain of my for loop's & while loop. The reason being I have never had so many loops in a function before... I need to know if these for loops will work ... So any advice on making the loop's more condensed or of any different way, would be great ...

Code:
# Builds the packages & then copies to your CLFS partition

buildpkg()
{
  
                       cd $abs_bd 
		    for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
		    do
			cd $pkg 
			    makepkg -s $pkg || pkgfail  # Rebuild function
			cd ..  
		    done   

E=0
while [ $E -eq 0 ]; then
 
  cat << END 
Now that all packages have been built, we need to know where to copy the built 
packages. Please specifiy where to copy your built packages.

e.g.   /mnt/clfs/sources/pacman

END

    read clfs_dir
	   cd $clfs_dir   
				if [ -d $clfs_dir ]; then 
					break
				else
				    clear && echo "INVAILD DIRECTORY" && sleep 1s && clear && E=0
				fi
done

                cd $abs_bd  # <--Seems redundant, but unsure
	     for mpkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;
		do
		    cd $mpkg
		          cp *x86_64.pkg.tar.gz $clfs_dir
		     cd ..   
                done
}
 
Old 10-07-2009, 12:19 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
If you re-format that, it'd be easier to read.

Anyway
Code:
for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
do
    cd $pkg
is a problem. Once you cd into a sub-dir, it'll assume the next dir is a sub-sub-dir etc ie its going to try to

cd acl
acl> cd attr
acl/attr>cd fakeroot

etc. not what you meant. You need to 'cd ..' at the end of the loop to get back to '$abs_bd' or 'cd $abs_bd' .

This
cd $clfs_dir
is redundant; just test if it's a dir, no point in cd'ing into it (especially as the dir test comes after that line; too late...
 
Old 10-07-2009, 01:04 AM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by chrism01 View Post
If you re-format that, it'd be easier to read.
Ya the formating was a copy and paste from my IDE ... its much easier to read in my app.

Quote:
Originally Posted by chrism01 View Post
is a problem. Once you cd into a sub-dir, it'll assume the next dir is a sub-sub-dir etc ie its going to try to

cd acl
acl> cd attr
acl/attr>cd fakeroot

etc. not what you meant. You need to 'cd ..' at the end of the loop to get back to '$abs_bd' or 'cd $abs_bd' .
That makes sense... it was cd'ing into a sub-dir... This is how it was, can you give me an example...
Code:
 
cd $abs_bd 
for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
do
  cd $pkg 
  makepkg -s $pkg || pkgfail  # Rebuild function
  cd ..   # Doesn't this return to $abs_bd ..?
done
This seemed like it cd .. into the next dir, followed in the loop ...
Is this what you meant..?
Code:
makepkg -s $pkg && cd .. || pkgfail  # Rebuild function
Quote:
Originally Posted by chrism01 View Post
This
cd $clfs_dir
is redundant; just test if it's a dir, no point in cd'ing into it (especially as the dir test comes after that line; too late...
I tried using my [-d] test w/o the cd, and for some reason it would just loop in my "while", so I had to add a "cd $clfs_dir" in order for the test statement to work ... odd

Thanks for your patience ... You've helped me on other occasions...

With only a few months combined experience with Bash, I'm slowly grasping the basics.
 
Old 10-07-2009, 01:27 AM   #4
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 444

Rep: Reputation: 98
Code:
cd $abs_bd 
for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
do
  cd $pkg 
  makepkg -s $pkg || pkgfail  # Rebuild function
  cd ..   # Doesn't this return to $abs_bd ..?
done
to be sure that you're in the right directory
you could use absolute paths, eg:
Code:
cd $abs_bd
CWD=$PWD
for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
do
  cd $CWD/$pkg 
  makepkg -s $pkg || pkgfail  # Rebuild function
  cd $CWD   # this does return to $abs_bd
done
 
Old 10-07-2009, 01:43 AM   #5
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by vonbiber View Post
Code:
cd $abs_bd
CWD=$PWD
for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
do
  cd $CWD/$pkg 
  makepkg -s $pkg || pkgfail  # Rebuild function
  cd $CWD   # this does return to $abs_bd
done
Brilliant ... I didn't think of pwd ... Thanks man ... this should work nicely.
 
Old 10-07-2009, 05:07 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
cd ..
means go up one level ie to the parent dir; which where you came from as you are not using absolute paths for the acl attr fakeroot pacman libfetch dirs there.

You really just need to decide if you are going to 'know where you are' and use relative paths, or assume the worst and use absolute paths.
It's important to understand the pwd thing clearly.
I'd recommend

set -xv

at the start to see exactly what's happening.
 
  


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
Need LVM sanity check flashl Fedora 6 07-16-2007 08:56 PM
Sanity Check DotHQ Linux - General 6 03-31-2006 09:24 PM
./configure sanity check xushi Slackware 9 06-18-2005 04:47 PM
cpp sanity check alexrait1 Slackware 6 04-24-2005 08:43 AM
sanity check failed for g++ pablovschby Programming 2 11-08-2004 02:39 AM

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

All times are GMT -5. The time now is 03:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration