ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As I said here I am trying to exercise the little I know about shell scripts. I have a few questions:
1 - I finished all scripts for Chapter 5, I have a directory with 33 shell scripts listed from 01 to 33. Like this:
Code:
pedro@slack:~/Scripts/LFS7.6-rc1/Chapter5$ ls -lh
total 132K
-rw-r--r-- 1 pedro users 567 Set 10 16:36 01-binutils-pass1.sh
[...]
-rw-r--r-- 1 pedro users 49 Set 10 18:09 33-changingownership.sh
How would I run them in order? Without having to run them manually? I was imagining something like "for 01 to 33, do" (I know bash/shell loops) but I am not really sure how to do this.
2 - I didn't put a license on them, how would I add the same license header on all of them? Without doing it manually of course.
3 - What would be the best way to indent them?
4 - Once released, could you take a look at them? I plan on hosting them at github or something like that.
5 - They are very silly, basically just a copy paste off the book but it saves some time. Example of one of them:
Code:
#!/bin/sh
set -e
LFS_TGT=$(uname -m)-lfs-linux-gnu
name=glibc
version=2.20
tar vxf $name-$version.tar.xz
cd $name-$version
if [ ! -r /usr/include/rpc/types.h ]; then
su -c 'mkdir -pv /usr/include/rpc'
su -c 'cp -v sunrpc/rpc/*.h /usr/include/rpc'
fi
mkdir -v ../$name-build
cd ../$name-build
time {
../$name-$version/configure \
--prefix=/tools \
--host=$LFS_TGT \
--build=$(../$name-$version/scripts/config.guess) \
--disable-profile \
--enable-kernel=2.6.32 \
--with-headers=/tools/include \
libc_cv_forced_unwind=yes \
libc_cv_ctors_header=yes \
libc_cv_c_cleanup=yes
make
make install; }
cd ../
rm -rf $name-$version $name-build
Thanks.
Last edited by moisespedro; 09-10-2014 at 04:20 PM.
Reason: Finished
And about license header I should have worded it better. What I meant to say is that I want to add a license text before "#!/bin/sh" on all files. But I've found that on Google already. It seems an echo + cat or a sed will do the job
Actually I couldn't get it working. I am trying to insert the contents of a file (which I called LICENSE.TXT) to the beggining of all .sh files (before the #!/bin/sh). I tried a few things I found in stackoverflow but I couldn't get it.
The archive is GPL 2 header:
Code:
one line to give the program's name and an idea of what it does.
Copyright (C) yyyy name of author
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
I believe the shebang has to be the first line, nothing can come before it. With that in mind, here's a little script that would add the contents of a file into a shell script after the shebang (if it exists), but before anything else:
Code:
#!/bin/bash
nargs=$#
if [[ $nargs -lt 2 ]]; then
echo "Usage: $0 license_file script_file(s)"
exit 1
fi
license_file="$1"
shift
tempfile=$(mktemp)
while (( "$#" )); do
if [[ -f $1 ]]; then
rm $tempfile
shebang=1
license_done=0
while read line; do
if [[ $shebang -eq 1 && ${line:0:2} == '#!' ]]; then
echo $line > $tempfile
else
shebang=0
fi
if [[ $license_done -eq 0 ]]; then
while read lline; do
echo '#'$lline >> $tempfile
done < $license_file
license_done=1
fi
if [[ $shebang -eq 0 ]]; then
echo $line >> $tempfile
fi
shebang=0
done < $1
mv $tempfile $1
fi
shift
done
rm $tempfile
save it as some file in your PATH, say "~/bin/insert_header", and then you can apply it to any script you want
Code:
for file in *.sh; do
insert_header /path/to/header_file $file
done
edit: this script will change the permissions of the file you're modifying though (it will remove execute permissions), which obviously is a problem and will need to be corrected. I'll leave that exercise up to you
As you have identified, your current scripts simply are a cut and paste of the original and hence as commands change or you decide to use new ones (the ones that install the scripts) you would need to go through and update
all 33 or at least all calling the same command with the new option or new command.
As the general format for scripts in LFS is as follows:
patch
scripted changes
configure
make
make test
make install
post install changes
Why not try and write something that follows this process and then calls (read as sources) configuration files to populate variables in your base install script.
This way you could also drive the base script off an install file which advises the order the configuration files would be called in.
It worked but I had to comment all the lines involving $tmpfile. I was getting errors like "Can't remove /tmp/tmpsomething, file not found"
Code:
pedro@slack:~/Scripts/LFS7.6-rc1/Chapter5$ for file in *.sh; do ~/insertheader.sh LICENSE.TXT $file; done
rm: não foi possível remover “/tmp/tmp.N68xso”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.8uCbyq”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.Hb470h”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.AE3Olh”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.38LLCj”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.4MtJVn”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.dr00Cp”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.5c5W8n”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.0BONos”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.qCE6qu”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.nXSiet”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.zGXNTv”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.jAAMez”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.Q5yp7x”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.DeOGvC”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.kuUFAB”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.sr8GiD”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.gk8TsH”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.eBU0nJ”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.2DeSbI”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.D284vL”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.pTK7nK”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.uMVU3O”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.labwTQ”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.8W13qQ”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.Ge6raS”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.RQNlqV”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.ZosxkU”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.fmEeWX”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.drzQTW”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.9qIN00”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.AHGJ32”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.yM7qH1”: Arquivo ou diretório não encontrado
rm: não foi possível remover “/tmp/tmp.8ib0p5”: Arquivo ou diretório não encontrado
Since I have backups I commented the lines out (if something went wrong I am fine) and it worked like a charm, thanks ^^.
As you have identified, your current scripts simply are a cut and paste of the original and hence as commands change or you decide to use new ones (the ones that install the scripts) you would need to go through and update
all 33 or at least all calling the same command with the new option or new command.
As the general format for scripts in LFS is as follows:
patch
scripted changes
configure
make
make test
make install
post install changes
Why not try and write something that follows this process and then calls (read as sources) configuration files to populate variables in your base install script.
This way you could also drive the base script off an install file which advises the order the configuration files would be called in.
Just a thought
I was thinking about doing something a bit more clever but it is beyond my knowledge now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.