LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2014, 04:13 PM   #1
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Rep: Reputation: 195Reputation: 195
A few questions about shell scripts


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
 
Old 09-10-2014, 04:26 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
1 - I can think of a couple
Code:
for i in *.sh; do ./$i; done
Code:
for i in {01..33}; do ./${i}*; done
2 - Not sure what you mean, what license header?

3 - Open it up in emacs
Ctrl-x h
Ctrl-Alt-\
Ctrl-x-s
Ctrl-x k Enter

Last edited by suicidaleggroll; 09-10-2014 at 04:27 PM.
 
Old 09-10-2014, 05:21 PM   #3
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
The first for loop will run the files in alphabetical order?

EDIT: I used the second one, I did a quick test here and the first one would run the 00 script again (the script that will run them all)

Last edited by moisespedro; 09-10-2014 at 05:26 PM.
 
Old 09-10-2014, 05:37 PM   #4
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
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
 
Old 09-10-2014, 05:56 PM   #5
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
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.
 
Old 09-10-2014, 07:14 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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

Hint: http://askubuntu.com/questions/15200...m-command-line

Last edited by suicidaleggroll; 09-10-2014 at 07:23 PM.
 
1 members found this post helpful.
Old 09-10-2014, 11:44 PM   #7
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Thank you very much, sir. Will try it tomorrow.
Well, I will just need to "chmod +x" them again
 
Old 09-11-2014, 01:34 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 09-11-2014, 11:51 PM   #9
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
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 ^^.
 
Old 09-11-2014, 11:54 PM   #10
moisespedro
Senior Member
 
Registered: Nov 2013
Location: Brazil
Distribution: Slackware
Posts: 1,223

Original Poster
Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by grail View Post
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.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make these shell scripts with ash as my shell? riahc3 Programming 13 08-15-2012 03:57 AM
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Two questions about scripts gubak Linux - Newbie 5 05-10-2007 09:03 AM
shell scripts gui10 Programming 10 10-28-2001 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 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
Open Source Consulting | Domain Registration