Bash Scripting - How would you like to Learn?

So it has been suggested that I consider starting a blog on Bash scripting. I have spent a far amount of time lately
working on and posting bash scripts here in the openSUSE forum in the hope that you might find something useful to you.
I don’t think such efforts would make me any sort of bash scripting expert, but what if our goal is to help each other
instead of just one master showing everyone else how it is done? In fact I have said more than once here in the forum
that when you try to help someone else, you will be in fact helping yourself as well. Is it possible we could also apply
such a task to learning bash scripting? Now we really do have some very good scripting experts in this very forum and
if we are lucky, perhaps we could get them in here to help. To get started though, we need some suggestions on what
kind of tasks we want to perform while learning bash scripting? What I have determined about most self teaching methods
is that we need a useful goal, some task we want to automate using bash scripting. It needs to be of interest to lots
of people, but not so difficult as to cause our readers to lose their interest in the task of learning. So to start off
with this task of learning bash scripting I need your ideas and I need your support. What do you want to do, that might
be interesting to lots of people? What goal have you set for your self for bash scripting? Now, if you have seen my
blogs here, you know I have an interest in writing bash scripts and I like to share them with others. Would you like
to help do the same here as well? Please, respond to this blog with any suggestions you might have which could help to
our common goal to learn how to write a bash script. For ideas, look around your daily tasks using openSUSE. How many
things do you find yourself doing over and over? Have you ever consider automating it? Have you ever tried to write a
Bash script before? I can tell you that it can be exciting when you manage to get a program that you wrote working. It
can be exciting when you find out that another fellow Linux user is using your program on his or her PC and is happy to
be using it. All thanks to you and your help. If you are interested, please let me know as soon as you read this message.

Thank You,

Blogs: asroot : Bash : Packet Filter : C.F.U. : GPU’s : fewrup : F.S.M. : H.I. : nVIDIA : LNVHW : N.S.F. : S.A.K.C. : MMCHECK
S.A.S.I. : S.C.L.U. : S.G.T.B. : S.K.I.M. : S.L.A.V.E. : S.L.R.C. : S.T.A.R.T. : S.U.F.F. : SYSEdit : systemd : YaST Power

I’m such a newbie at bash, that any starting point would be good. I don’t know how simple my ideas would be, but perhaps we could take your sakc script and integrate it with hwinfo so that we can get a kernel compiled specifically for the hardware of that system. Since your more versed than I, I’ll let you decide on that one. But that would be a cool goal to have.

Well sakc is not a starter bash script even though the kernel compile process has just four main tasks to perform. I am always interested in adding in function to sakc, but I think something more simple would be best. We could even dissect something simple. I do know that one important aspect is the over all form of a bash script, what is good to do and what is a must to do in creating a bash script? It is early morning here and I can spend more time on this tonight. Lets keep thinking about this and thanks so much for your comments.

Thank You,

So lets get started with a list of bash commands that you can use. I found a really good listing here:

bash commands - Linux MAN Pages

I would also suggest you get my New Script File script and install it. You can find it here:

N.S.F. - New Script File, Bash Script File Header Creator - Version 2.6 - Blogs - openSUSE Forums

Here is one of many guides for someone that might be new to using bash scripting:

Bash Guide for Beginners

Now here is an example very first script you can write. I used the nsf bash script to create it:

#!/bin/bash

#: Title       : myscript
#: Date Created: Wed Jul 13 17:54:21 CDT 2011
#: Last Edit   : Wed Jul 13 17:54:21 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : 
#: Options     : 

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#

function show_gpl {
echo ""
echo "myscript is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

clear
echo
echo "This is my first bash script!"

show_gpl

exit 0

# End Of Script

Please let me hear any comments that you might have.

Thank You,

I recently read a nice scripting article in Admin Network & Security Issue 04 about Easy Scripting using grep, awk and sed. To quot a few short passages from that article:

“Grep’s function is to locate lines matching a specified pattern within one or a set of files (or standard input. It has the following general syntax:”

grep regular-expression files(s)

You can look up the grep command by typing in “man grep” at the terminal prompt to see more information on the subject. As an example of how I have recently used grep here is a single line from my bash script called suff, used to format USB floppy disks:

floppydrive=$(ufiformat --inquire --verbose | grep /dev/)

If I attach a USB floppy disk and run the following command, here is what I get:

james@Linux-Master:~> sudo ufiformat --inquire --verbose
list usb fdd devices
disk         generic     
/dev/sdk     /dev/sg12

Now, if I add a grep command here is what I get:

james@Linux-Master:~> sudo ufiformat --inquire --verbose  | grep /dev/
/dev/sdk     /dev/sg12 

In the second instance, now I am only getting the two USB floppy drive designations, either of which I could use and assigning this to the variable I called floppydrive. Next, lets look at awk and another quot from Admin…

“The awk command takes a set of internal commands to execute - known as an awk script - as its first argument, followed by one or more file names, when not part of a pip. Alternatively, the script can be stored in an external file and specified to awk with the -f option.” You can look up the awk command by typing in “man awk” at the terminal prompt to see more information on the subject.

Now, lets look at how I used awk again in the suff bash script with the following line that said:

floppydrive=$(echo $floppydrive | awk '{print $1}')

To properly demonstrate its operation, lets add it to the end of the previous command:

sudo ufiformat --inquire --verbose  | grep /dev/ | awk '{print $1}'
/dev/sdk

The difference now is we only get the first entry of /dev/sdk, which is all we really needed to use in the first place. Now in my script, I ran into an issue I could not discern when a floppy disk was not the drive and I had to separate the last command, taking another line to complete once the lack of a floppy disk check was complete. Now you can see though how you can use awk and even combine it with grep when needed.

The next command is sed. To quote Admin a last time, “The sed utility is a stream editor designed to transform input according to the specifications it receives.” You can look up the sed command by typing in “man sed” at the terminal prompt to see more information on the subject. Again, in suff here is a line where I use the sed command:

format$start]=$(ufiformat -i $floppydrive | grep formattable | sed -n $(( start ))p

To run this command at the terminal prompt here is what I did and what I got.

james@Linux-Master:~> sudo ufiformat -i /dev/sdk | grep formattable | sed -n 1p
formattable  2880  512 1440

If I remove the sed command, then here is what I get:

james@Linux-Master:~> sudo ufiformat -i /dev/sdk | grep formattable
formattable  2880  512 1440
formattable  1232 1024 1232
formattable  2400  512 1200

In this example, sed is allowing me to pull out just the line I want to use from three lines being provided by the ufiformat command. In our example from suff, I had to use the sudo command only because ufiformat can’t access your floppy drive as a normal user.

So, we have looked at grep, awk and sed, all are useful for manipulating the text output of other terminal commands which we can then sift through for just the info we want to use. As always, please post your comments to our lesson today.

Thank You,

Did you know that you can send a bash command to operate in the background while the rest of the script is running in the foreground? Specifically, New jobs always run in the foreground unless you specify otherwise. To run a job in the background, end the input line with an ampersand (`&’). This is useful for running non-interactive programs that perform a lot of calculations. I have used in my bash script called sakc in order to display the compile time while the kernel is being compiled. Bash scripts run serially from top to bottom, though functions can be run out of order, but which must always be located above the bash code that calls it. In order to have multiple bash code commands running at the same time, you must force something into the background.

Here is my sakc example:

# ***********************************************************************************
#
# Time Display Function.  Do you want the Elapsed time displayed while your compile is running?  
#
show_time=true

#
# Display Elapsed Time While doing the compile / This runs in the background
#
function time_display {
  tput cup $(( 14 + line_os)) $(( 41 + col_os ))
  echo -n "Elapsed Time: 0   minute(s) "
  while true ; do let comp_time=comp_time+1 && sleep 60 && tput cup $(( 14 + line_os)) $(( 55 + col_os )) && echo -n "$comp_time$(tput sgr0)" ; done &
PID1=$!
}

#
# Start Time Display if enabled
#
if $show_time ; then
  time_display
fi

#
# Kill Background Elapsed Time Display shown during the compile
#
if $show_time ; then
  disown $PID1
  kill -9 $PID1
fi

#*****************************************************************************************

Once a minute, the bash command running in the background updates the minute read out as to how long the compile has run. The compile process is running in the foreground while the time display is running in the background. Without forcing the time display into the background, I could not display the time once a minute after transferring the foreground task to the kernel compiler. It is important to note I saved the process PID # as the variable PID1=$!. By doing so, I can then stop or KILL the process when I no longer want the compile time to be displayed. You must save the PID number in order to know what to kill. In order to stop the time display from working I must first use the command disown $PID1. You can not kill a process that is owned by another like the sakc script that started it. Next, once it is no longer owned by anyone, it can be killed by command kill -9 $PID1.

So, to go back over the steps to place a bash command into the background.

  1. You need a command that does not need interaction from the foreground to run and may include calculations or other such tasks.
  2. You must place the ampersand (`&’) as the very last item on that line.
  3. If you need to stop the line from running because it does not stop on its own like my sakc time display save its PID # with the command: PID1=$!
  4. To KILL a background task you must first disown it with the command: disown $PID1
  5. Once a background task is no longer owned by a foreground task it can be killed by the command: kill -9 $PID1
  6. The name PID1 in my example can be any variable name you wish to use.
  7. If the background task you start ends on its own, there will be no need to kill it unless you do so when your script is stopped.

That is our lesson for the today. As always, I would appreciate you comments…

Thank You,

What about

>source sript-name.sh

Why would one want to run that and what are the advantages or disadvantages? I’d like to know some uses, real cases for that.

Taken from Chapter 2 of Bash Guide for Beginners Creating and running a script

So after reading your link and playing with the source command I found several interesting finds…

  1. The target file name for source does not have to be marked as executable as is the case with normal bash scripts.
  2. The bash script executes as normal
  3. Any variables set in the script are still set when it completes. Normally you must export variables to still be set when the script completes.
  4. The script executes in the same shell and does not spawn another.

So, using the source command seems most useful maintaining the variables that were set. Perhaps there are other reasons that do not come to mind at present.

Thank You,

Here is a simple bash script designed to list the present folders and allow you to change the default folder. You must run this from a terminal prompt. Copy and past this text into text editor and save it as the script called subdir in the folder ~/bin/subdir (/home/yourname/bin/subdir):

#!/bin/bash

#: Title       : subdir
#: Date Created: Fri Oct 14 12:47:38 CDT 2011
#: Last Edit   : Fri Oct 14 12:47:38 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : Change default Folder from List
#: Options     : None

# To Use subdir to change the default folder, you must start the script using: . subdir

TITLE="Folder Selection Menu for:"

declare -a folders

gui=true

while $gui ; do

  size=$(ls -dl */ | grep -c /)
  let size=size+1
  clear
  echo "$TITLE $PWD"
  echo

  inc=1
  while  $inc -le $size ] ; do
    if  $size -gt 1 ] ; then
      folders$inc]="$(ls -dl */ | grep /| sed -n $(( inc ))p | awk '{print $9}')"
    fi
    if  $inc -lt $size ] ; then
      echo "$inc) ${folders$inc]}"
    else
      echo "$inc) .."
    fi
    let inc=inc+1
  done

  echo
  echo -n "Enter the folder # to select (Q=Quit & B=Back): "
  read CHOICE
  if  $CHOICE =~ ^[0-9]+$ ]] ; then
    if  $CHOICE -le $inc ]] &&  $CHOICE -gt 0 ]]; then
      if  $CHOICE -lt $size ] ; then      
    cd ${folders$CHOICE]}
      else
    cd ..
      fi
    fi
  else
    if  $CHOICE == [Qq] ]] ; then
        gui=false
    fi
    if  $CHOICE == [Bb] ]] ; then
      cd ..
    fi
  fi

done

# End Of Script

Once you have saved the script you must mark it executable. Run the following terminal command to do so:

chmod +x ~/bin/subdir

To use the new script, open up a terminal session and enter the command (That is a dot, then a space and the name subdir):

. subdir

Then, run it again, but without the . as in

subdir

Play around with the script and see what it does. It is very simple, but does show some issues with leaving things like the default folder as is when a script finishes. Without starting using the ., no matter the folder selected, you are back to home when you exit the script. The . allows the script to run in the same shell as the terminal you just opened up.

Please I want to hear any comments that you might have.

Thank You,

James

Function Calls are very powerful when used in a bash script. Simply put, for any task you must use more than once in a script, create a function call for it, place the repetitive code within and use it throughout your script. For a function call to be used, it must already appear in the script before you use it. So, for this to work, functions need to be defined at the top of your script. There are two basic ways to define a function:

#
# Comment on what this function does
#
function function_name {
command(s)...
}
#
# Comment on what this function does
#
function_name () {
command(s)...
}

I prefer to use the first, but if you program in C a lot, you may want to use the second way. Functions can not be blank, some command must exist within. A function call can use another function call, but the function used must always be read first in the script or above where it is used. Once a function call is defined, to use it in your script, you simply refer to it by name. As in …

function_name

Your script then runs all of the commands in the function and then returns back to where it left off in the script. Once all commands have been run, there is an implicit return command assumed. However, you may add a return command if you wish.

#
# Comment on what this function does
#
function function_name {
command(s)...
return [number]
}

When you exit a function, you may return with a number, like an exit code for instance. The return value must be numeric and can not be text. To read the number on return, use the following method:

function_name
Return_Number=$?

Any variables you have defined before running the function, can be seen and used by the function. Further, unless defined as local, any changes to a variable will be in effect when you return from the function. Be careful of using the same variable over and over as a function will modify its value. For common values, you want to use over and over in a function without changing the value somewhere else in your script, declare it as being local.

#
# Comment on what this function does
#
function function_name {
local loc_var=23
command(s)...
return [number]
}

In this example, the variable we called loc_var in the function_name will not be effected when used anywhere else in the script. If we want to define values to be sent to the function call, we can place those on the command line with the name of the script.

function_name MyName YourName HisName

In this example, when the function named function name is run, $1=MyName, $2=YourName and $3=HisName. If there are spaces in the value you wish to pass, place it in quotations. as in.


function_name "My Name" "Your Name" "His Name"

There is no reason to not place all variables sent to your functions into quotes. Then, no matter what, they will be passed properly. So in the case of using the function call.

function_name "My Name" "Your Name" "His Name"

In the function,

$1=“My Name”
$2=“Your Name”
$3=“His Name”

Since scripts also use $1, $2 and $3 for the script command line options that start up your script, you can’t use the script startup parameter $1 directly without passing them on the command line. For the script $1 to be equal to the function call $1, I would have to do this.

function_name "$1"

Thus, $1 to the script would now be $1 in the function call of the same script, if that is what you wanted to do. Let us suppose I wanted to add color to my script using the tput commands. The command tput setf # sets the forground color while tput setb # sets the background color. Using a function, I could simply change the command to a single entry in my script:

#
# Color Display Request - color foreground background 
#
# 0: Black 1: Blue 2: Green 3: Cyan 4: Red  5: Magenta 6: Yellow 7: White
function color {
  tput setf $(( $1 ))
  tput setb $(( $2 ))
  return 0
}

Now, to create a color line of text in my bash script I could use the command:

color 7 2
echo "This is text=White, Background=Green"
color 7 0
echo "This is text=White, Background=Black"

As you can see, using function calls in your bash script can make them very powerful. I hope you have found this entry to be useful to you today. As always, I welcome your comments.

Thank You,

So much of writing a bash script has you doing two things:

  1. Using a collection terminal programs and commands
  2. Deal with and using text output from those commands.

Lets take the output from the command /sbin/ifconfig. Open up a terminal session and run this command and see what you get:

/sbin/ifconfig

eth0      Link encap:Ethernet  HWaddr BC:AE:C5:78:12:8E  
          inet addr:192.168.0.185  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::beae:c5ff:fe78:128e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4716178 errors:0 dropped:5 overruns:0 frame:0
          TX packets:2562693 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6832101413 (6515.5 Mb)  TX bytes:207334947 (197.7 Mb)
          Interrupt:66 Base address:0x4000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:99067914 errors:0 dropped:0 overruns:0 frame:0
          TX packets:99067914 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:6192194383 (5905.3 Mb)  TX bytes:6192194383 (5905.3 Mb)

Now suppose we want to fetch the ip address of our hard wired port eth0. Here is the line we are interested in getting: inet addr:192.168.0.185 Bcast:192.168.0.255 Mask:255.255.255.0 so how might we get only this information? Lets try using grep:

/sbin/ifconfig | grep "inet addr:"

          inet addr:192.168.0.185  Bcast:192.168.0.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

Seems like we got more than we want. But look, the line we want has an additional word not present in the second line. Do you see it? Yes its the word “BCast:”. So, using that lets see what we can get.

/sbin/ifconfig | grep "inet addr:" | grep "Bcast:"

inet addr:192.168.0.185  Bcast:192.168.0.255  Mask:255.255.255.0

Now that is more like it, but suppose we only want the IP address and that is all. Lets try this command:

/sbin/ifconfig | grep "inet addr:" | grep "Bcast:"| awk '{print $2}' | cut -d \: -f 2

192.168.0.185

Now, you can see how combining bash commands using the pipe symbol “|” can be used to deal with the text and get only the output that you want. Now be aware if you have two network adapters, like wired and wireless working at the same time, you would actually get two IP addresses here and not just one. We can use a different command, looking only for ethx & wlanx names to fetch the real port name(s):

/sbin/ifconfig | awk '{print $1}' | grep -e "eth" -e "wlan"

eth0

Now, how might we put all of this together in a script that might be usable? Here is one I called myip:

#!/bin/bash

#: Title       : myip
#: Date Created: Sat Nov 5 13:05:27 CDT 2011
#: Last Edit   : Sat Nov 5 13:05:27 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : 
#: Options     : 

declare -a ipadd
declare -a netadp

#
# Color Display Request - color forground background ** ********************
#
# 0:Black 1:Blue 2:Green 3:Cyan 4:Red  5:Magenta 6:Yellow 7:White
function color {
  if  $3 == [Bb] ]] ; then
    tput bold
  fi
  if  $3 == [Nn] ]] ; then
    tput sgr0
  fi
  if $use_color ; then
    tput setf $(( $1 ))
    tput setb $(( $2 ))
  else
    tput setf 7
    tput setb 0
  fi
  return 0
}

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#

function show_gpl {
echo ""
echo "myip is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

size=$(/sbin/ifconfig |  grep "inet addr:" | grep -c "Bcast:" )
inc=1
if  $size -ge 1 ] ; then
  while  $inc -le $size ] ; do
    ipadd$inc]=$(/sbin/ifconfig | grep "inet addr:" | grep "Bcast:"| awk '{print $2}' | cut -d \: -f 2 | sed -n $(( inc ))p )
    netadp$inc]=$(/sbin/ifconfig | awk '{print $1}' | grep -e "eth" -e "wlan" | sed -n $(( inc ))p)
    echo
    color 7 0 B
    echo -n "Network Interface: "
    color 6 1 B
    echo -n "${netadp$inc]}"
    color 7 0 B
    echo -n " with IP address: "
    color 6 1 B
    echo -n "${ipadd$inc]}"
    color 7 0 N
    echo
    echo
    let inc=inc+1
  done
else
  echo "Sorry, No IP addresses were found!"
  exit 1
fi

exit 0

# End Of Script

Copy and past this script into a file in the ~/bin folder as myip. Open a terminal session and run the command: chmod +x ~/bin/myip

That is all for today…

Thank You,**

I had to post this link somewhere (so I know where to find it back :wink:

Advanced Bash-Scripting Guide

But it would be probably more useful if it would appear as a link and people could just click on it.

I had to post this link somewhere (so I know where to find it back :wink:

Advanced Bash-Scripting Guide

But it would be probably more useful if it would appear as a link and people could just click on it.

The best I could do was to post it inside of a message from me.

Thank You,

The following bash script will download most of the online bash scripts I have posted here. Copy the text and save it as the text file I called bsl into your ~/bin folder (/home/yourname/bin/bsl):

#!/bin/bash

#: Title       : bsl
#: Date Created: Sun Sep  2 15:25:45 CDT 2012
#: Last Edit   : Fri Jul 26 19:40:51 CDT 2013
#: Author      : James D. McDaniel
#: Version     : 1.67
#: Description : Download 27 Bash Scripts
#: Options     : None

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#

function show_gpl {
echo ""
echo "bsl is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2012 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

clear
echo "Bash File Script Loader"
echo
echo "This will install 27 bash scripts.  Some require root authority to be installed. None"
echo "of the scripts will be executed.  You are encouraged read through this script and remark"
echo "out anything you do not want to download.  You need internet access for this to work."
echo
read -p "Are you ready to install these bash scripts? (y/n) (n): " CHOICE

if  $CHOICE != [yY] ]] ; then
  exit 0
fi

# Grub 2 Utility (1) 

rm ~/bin/grub2cmd ; wget -nc http://paste.opensuse.org/view/download/58627662 -O ~/bin/grub2cmd ; chmod +x ~/bin/grub2cmd

# systemd Utility (26)

rm ~/bin/sysdcmd ; wget -nc http://paste.opensuse.org/view/download/95655714 -O ~/bin/sysdcmd ; chmod +x ~/bin/sysdcmd

# zypper Utility (27)

sudo rm /usr/local/bin/zc ; sudo wget -nc  http://paste.opensuse.org/view/download/75755470 -O /usr/local/bin/zc ;  sudo chmod +x /usr/local/bin/zc

# Samba Setup Script (2)

rm ~/bin/sact ; wget -nc http://paste.opensuse.org/view/download/60035 -O ~/bin/sact ; chmod +x ~/bin/sact

# User Utility bash scripts for everyone (2,3,4,5,6,7)

rm ~/bin/safp ; wget -nc http://paste.opensuse.org/view/download/29253557 -O ~/bin/safp ; chmod +x ~/bin/safp

rm ~/bin/isomount ; wget -nc http://paste.opensuse.org/view/download/28322886 -O ~/bin/isomount ; chmod +x ~/bin/isomount

rm ~/bin/sclu ; wget -nc http://paste.opensuse.org/view/download/85914606 -O ~/bin/sclu ; chmod +x ~/bin/sclu

rm ~/bin/fsm ; wget -nc http://paste.opensuse.org/view/download/53978354 -O ~/bin/fsm ; chmod +x ~/bin/fsm

rm ~/bin/slrc ; wget -nc http://paste.opensuse.org/view/download/89529032 -O ~/bin/slrc ; chmod +x ~/bin/slrc

rm ~/bin/suff ; wget -nc http://paste.opensuse.org/view/download/903210 -O ~/bin/suff ; chmod +x ~/bin/suff

# Add "My Computer" icon back to openSUSE 12.3 (8)

rm ~/bin/saksi ; wget -nc http://paste.opensuse.org/view/download/91677878 -O ~/bin/saksi ; chmod +x ~/bin/saksi

# System Utility Checkers (9,10,11,12,13,14)

rm ~/bin/hi ; wget -nc http://paste.opensuse.org/view/download/27608571 -O ~/bin/hi ; chmod +x ~/bin/hi

rm ~/bin/slave ; wget -nc http://paste.opensuse.org/view/download/50521534 -O ~/bin/slave ; chmod +x ~/bin/slave

rm ~/bin/start ; wget -nc http://paste.opensuse.org/view/download/49185505 -O ~/bin/start ; chmod +x ~/bin/start

rm ~/bin/skim ; wget -nc http://paste.opensuse.org/view/download/60388103 -O ~/bin/skim ; chmod +x ~/bin/skim

rm ~/bin/cfu ; wget -nc http://paste.opensuse.org/view/download/92162247 -O ~/bin/cfu ; chmod +x ~/bin/cfu

rm ~/bin/mmcheck ; wget -nc http://paste.opensuse.org/view/download/17784442 -O ~/bin/mmcheck ; chmod +x ~/bin/mmcheck

# Write your own Bash scripts (15)

rm $HOME/bin/nsf ; rm $HOME/Desktop/NSF.desktop

sudo rm /usr/local/bin/nsf ; sudo wget -nc   http://paste.opensuse.org/view/download/27761727 -O /usr/local/bin/nsf ;  sudo chmod +x /usr/local/bin/nsf

# This is the old 2.xx version: rm ~/bin/nsf ; wget -nc   http://paste.opensuse.org/view/download/51163610 -O ~/bin/nsf ; chmod  +x ~/bin/nsf

# Root User Substitution (16)

rm ~/bin/asroot ; wget -nc http://paste.opensuse.org/view/download/34473473 -O  ~/bin/asroot ; chmod +x ~/bin/asroot

# System File Editing scripts (17,18)

rm $HOME/bin/sysedit ; rm $HOME/Desktop/SYSEdit.desktop

sudo rm /usr/local/bin/sysedit ; sudo wget -nc  http://paste.opensuse.org/view/download/88360942 -O  /usr/local/bin/sysedit ; sudo chmod +x /usr/local/bin/sysedit

rm ~/bin/fewrup ; wget -nc http://paste.opensuse.org/view/download/23644270 -O ~/bin/fewrup ; chmod +x ~/bin/fewrup

# Getting the After.local bash script to work (19)

wget -nc http://paste.opensuse.org/view/download/33889052 -O ~/bin/make-after-local ; chmod +x ~/bin/make-after-local 

# Using DKMS, must install dkms first (20)

wget -nc http://paste.opensuse.org/view/download/71295014 -O ~/bin/make-dkms-installer ; chmod +x ~/bin/make-dkms-installer

# Compile Your Own Kernel (21,22,23)

rm ~/bin/sgtb ; wget -nc http://paste.opensuse.org/view/download/1617846 -O ~/bin/sgtb ; chmod +x ~/bin/sgtb

rm ~/bin/sakc ; wget -nc http://paste.opensuse.org/view/download/55036844 -O ~/bin/sakc ; chmod +x ~/bin/sakc

rm ~/bin/sakr ; wget -nc http://paste.opensuse.org/view/download/607174 -O ~/bin/sakr ; chmod +x ~/bin/sakr

# Load nVIDIA Video Driver (24,25)

sudo rm /usr/local/bin/lnvhw ; sudo wget -nc  http://paste.opensuse.org/view/download/40288603 -O /usr/local/bin/lnvhw   ; sudo chmod +x /usr/local/bin/lnvhw

sudo rm /usr/local/bin/sandi ; sudo wget -nc  http://paste.opensuse.org/view/download/88011425 -O /usr/local/bin/sandi  ; sudo chmod +x /usr/local/bin/sandi

show_gpl

exit 0

# End Of Script


To make it executable, run the following terminal command:

chmod +x ~/bin/bsl

Finally, to use the bash script, open up a terminal session and run the command:

bsl

This will install the following bash scripts into your ~/bin folder: sclu, asroot, cfu, fsm, hi, mmcheck, nsf, sakc, sgtb, skim, slave, slrc, start, suff, fewrup, sysedit and fastboot, & pbs are located in the /usr/local/bin folder. The next to the last script not installed by default is for only openSUSE 12.1 and so remove the comment if you want to load the after.local patch. The last script is not installed by default called lnvhw to allow the install of the nVIDIA driver the hard way. Remove the # at the first of the line to get this script before you run sgbs and good luck.

Thank You,

I came upon this little script that is intended to count the number of words you type per minute in a terminal console. It is real simple but it has one very interesting line in it that says:

speed=`echo "scale=2; $words / ( ( $end_time - $start_time ) / 60)" | bc`

Normally, in a bash script, all math is integer math.The original author is using the two utils called bc and scale. If you have had problems with integer math, why not check out these two utilities?

To create the following bash script, copy and paste the text in the code field below into a text editor and save it as the file I called typing_speed in your local home folder ~/bin (/home/username/bin/typing_speed):

#!/bin/bash

#: Title       : typing_speed
#: Date Created: Sun Jan 15 08:50:01 CST 2012
#: Last Edit   : Sun Jan 15 08:50:01 CST 2012
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : Count Words Per Minute
#: Options     : none

# Found at: http://www.linuxjournal.com/content/how-fast-can-you-type-develop-tiny-utility-bash-find-out

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#

function show_gpl {
read -p "Press Any Key to Exit" CHOICE
echo ""
echo "typing_speed is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

# speed.sh: a very tiny utility to measure typing speed.
clear
prompt="Start typing a piece of text. Press Ctrl-d to finish."
echo "$prompt"
echo
start_time=`date +%s`
words=`cat|wc -w`
end_time=`date +%s`
speed=`echo "scale=2; $words / ( ( $end_time - $start_time ) / 60)" | bc`
echo
echo "You have a typing speed of $speed words per minute."
echo
show_gpl

exit 0
#End Of Script

Once saved, open up a terminal session and type the following command to make it executable:

chmod +x ~/bin/typing_speed

To use the typing_speed bash script, open a terminal session and simply type:

typing_speed

Your speed counting begins at once. When done, just press ctrl-d and get your count at once.

Thank You,

Well, here is a little dity I found out today. Perhaps it is old hat to everyone else. Need a comprehensive listing of both internal and external terminal commands usable in a bash script on your PC? Just open up a terminal session AND:

Press TAB TAB, and you get this response: **Display all 4767 possibilities? (y or n)
**
Type in a y and get a listing that starts like this:

-                                            ijsgimpprint                                 ppdmerge
:                                            ilasm                                        ppdpo
!                                            ilbmtoppm                                    pphs
./                                           imagetops                                    ppltotf
../                                          imake                                        ppm3d
...                                          imgtoppm                                     ppmbrighten
                                            imlib2_bumpmap                               ppmchange
                                           imlib2_colorspace                            ppmcie
]]                                           imlib2_conv                                  ppmcolormask
{                                            imlib2_grab                                  ppmcolors
}                                            imlib2_poly                                  ppmdcfont
+                                            imlib2_show                                  ppmddumpfont
411toppm                                     imlib2_test                                  ppmdim
7z                                           imlib2_view                                  ppmdist
7za                                          import                                       ppmdither
a2disflag                                    in                                           ppmdmkfont
a2dismod                                     includeres                                   ppmdraw
a2enflag                                     indent                                       ppmfade
a2enmod                                      indxbib                                      ppmflash
a2p                                          inf2cdtext.pl                                ppmforge
a2ping                                       info                                         ppmglobe
a2ps                                         infocmp                                      ppmhist
a2ps-open                                    infokey                                      ppmlabel
aa-apparmor_notify                           infotocap                                    ppmmake
aa-apparmor_status                           infotopam                                    ppmmix
aa-audit                                     init_audigy                                  ppmnorm
aa-autodep                                   init_audigy_eq10                             ppmntsc
aa-complain                                  init_live                                    ppmpat
aa-decode                                    initviocons                                  ppmquant
aa-enforce                                   inixinfo                                     ppmquantall
aa-eventd                                    inkscape                                     ppmrainbow
aafire                                       inkview                                      ppmrelief
aaflip                                       innochecksum                                 ppmrough
aa-genprof                                   inputattach                                  ppmshadow
aainfo                                       install                                      ppmshift
aa-logprof                                   install_acx100_firmware                      ppmspread
aasavefont                                   installation_sources                         ppmtoacad
aatest                                       install-catalog                              ppmtoarbtxt
aa-unconfined                                install-catalog.sh                           ppmtobmp
ab2                                          install-dtd.sh                               ppmtoeyuv
abiword                                      install-info                                 ppmtogif
absset                                       install_intersil_firmware                    ppmtoicr
accept                                       installvst                                   ppmtoilbm

Keep pressing the space bar and be surprised at the total number of screens (37 on my setup) that appear one after another. Perhaps you have seen this or not. Now, does anyone know how to capture or print this listing by chance. If you do, post your answer in a reply to this comment.

Thank You,

Have a look at some new info useful in writing bash scripts here: https://forums.opensuse.org/blogs/jdmcdaniel3/linux-terminal-bash-script-command-redirection-your-text-display-explained-137/

Thank You,

Yes, you to can have an Easy to use Bash Menu Script. To use, copy the text in the code block below into any text editor:

#!/bin/bash

# *****************************************************************************
#: Title       : menudemo                                                     *
#: Date Created: Mon May 27 09:45:13 CDT 2013                                 *
#: Last Edit   : Mon May 27 09:45:13 CDT 2013                                 *
#: Author      : James D. McDaniel                                            *
#: Version     : 1.00                                                         *
#: Description : shows menu                                                   *
#: Options     : none                                                         *
# *****************************************************************************

TITLE=" menudemo - A Menu Demonstration Bash Script For You - Version 1.00 "
dash1="----------------------------------------------------------"

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#

function show_gpl {
echo ""
echo "menudemo is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2013 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

# *****************************************************************************
# Do you want to see menudemo in color? The default is true, but can be set  
# to false - New Escape Sequences works without a Desktop loaded 
# *****************************************************************************

use_color=true

# *****************************************************************************
# Color Display Request - color forground background ** 
#              Called from more than 10 locations in program
#
# 0:Black  1:Blue  2:Green  3:Cyan  4:Red  5:Magenta  6:Yellow  7:White
#
# Example: color 7 0 B 
#
# where forground is white (7), backgroud is black (0) and text is in bold (B)
#                          $1                      $2                      $3
# *****************************************************************************

function color {
  if $use_color ; then
    if  $3 == [Nn] ]] ; then
      tput sgr0
    fi
    if  $3 == [Bb] ]] ; then
      case "$1" in
        "0") echo -ne '\E[1;30m' ;;
        "1") echo -ne '\E[1;34m' ;;
        "2") echo -ne '\E[1;32m' ;;
        "3") echo -ne '\E[1;36m' ;;
        "4") echo -ne '\E[1;31m' ;;
        "5") echo -ne '\E[1;35m' ;;
        "6") echo -ne '\E[1;33m' ;;
        "7") echo -ne '\E[1;37m' ;;
        *) echo -ne '\E[1;37m' ;;
      esac
    else
      case "$1" in
        "0") echo -ne '\E[30m' ;;
        "1") echo -ne '\E[34m' ;;
        "2") echo -ne '\E[32m' ;;
        "3") echo -ne '\E[36m' ;;
        "4") echo -ne '\E[31m' ;;
        "5") echo -ne '\E[35m' ;;
        "6") echo -ne '\E[33m' ;;
        "7") echo -ne '\E[37m' ;;
        *) echo -ne '\E[37m' ;;
      esac
    fi
    case "$2" in
      "0") echo -ne '\E[40m' ;;
      "1") echo -ne '\E[44m' ;;
      "2") echo -ne '\E[42m' ;;
      "3") echo -ne '\E[46m' ;;
      "4") echo -ne '\E[41m' ;;
      "5") echo -ne '\E[45m' ;;
      "6") echo -ne '\E[43m' ;;
      "7") echo -ne '\E[47m' ;;
      *) echo -ne '\E[40m' ;;
    esac
  else
    echo -ne '\E[37;40m' 
  fi
  return 0
}

# *****************************************************************************
#                        Menu Display System function
#           Display up to $3 menu items and <enter> is always Exit 
#             Menu should have at least two choices plus exit
# *****************************************************************************

function print_menu {
  color 7 0 B
# You can adjust the starting position of the start menu.  
# Using s_line=0 & s_col=0 would place the menu in the top left corner of your screen"

# Starting Terminal Line Number for menu
  s_line=0
# Starting Terminal Column Number for Menu
  WIDTH=$(tput cols)

# Determine SuSE Version
  suse=$(< /etc/SuSE-release)
  version="${suse:0:14}"
  
# Main Program Title
  MTITLE="$1- $version"
  mtlen=${#MTITLE}
  
# Main String Width
  if [ $(($WIDTH)) -le  $mtlen  ] ; then
    s_col=0
  else
    s_col=$((($WIDTH - $mtlen) / 2 ))
  fi
  
  dashes="-----------------------------------------------------------------------------------------------------------"
  dashes="${dashes:0:mtlen}"

# Display Program Title
  tput clear
  tput cup $(( s_line )) $(( s_col ))
  color 2 0 B
  echo $dashes
  tput cup $(( s_line+1 )) $(( s_col ))
  color 7 2 B
  echo  "$MTITLE" 
  tput cup $(( s_line+2 )) $(( s_col ))
  color 2 0 B
  echo $dashes

# Determine Kernel Version
  kernel=$(uname -r)

# Display Menu, Present User home, kernel version
  SUBTITLE="$2 \E[1;37m- USER:$HOME - Kernel:$kernel $4"
  let slen=${#SUBTITLE}
  let slen=slen-6
  let slen=($mtlen - $slen)
  let slen=$(( slen / 2 ))  
  color 7 0 N
  tput cup $(( s_line + 4 )) $(( s_col + slen ))
  color 6 1 B
  echo  -ne " $SUBTITLE "
  color 7 0 N

# Display All Menu Options 1 through $3
  maxnum=$3
  counter=1
  let mlen=($mtlen - ${#5})
  let mlen=$(( mlen / 2 ))
  counter=0
  while [ $(( counter )) -lt $(( maxnum )) ] ; do
    tput cup $(( s_line + 6 + counter )) $(( s_col + mlen ))
    color 7 0 B
    opt="$5"
    ncolor=${#opt}
    color 7 0 B
    echo -n "${opt:0:4}"
    color 3 0 B
    echo "${opt:4:ncolor-4}"
    shift
    let counter=counter+1
  done

# Display menu option request and get user input
  color 6 1 B
  RTITLE="Enter Your Choice [1-"$maxnum"] ... OR ... Just Press <enter> to Exit this Menu:"
  let rlen=${#RTITLE}
  let rlen=rlen+2
  let rlen=($mtlen - $rlen)
  let rlen=$(( rlen / 2 ))    
  tput cup $(( s_line + 7 + counter )) $(( s_col + rlen ))
  echo -n " $RTITLE "
  if  $maxnum -gt 9 ] ; then
    read -n2 USERINPUT  
  else
    read -n1 USERINPUT
  fi
  if  $USERINPUT =~ ^[0-9]+$ ]] ; then
    if  $USERINPUT -le $maxnum ]] &&  $USERINPUT -ge 0 ]]; then
      tput sgr0
      color 7 0 B      
      return $USERINPUT
    else
      USERINPUT=""
    fi
  fi
  tput sgr0
  color 7 0 B  
  return $USERINPUT
}

# *****************************************************************************
#                     Main Menu Starts Here
# *****************************************************************************


MyVersion="- Anything Here"

show_menu1=true

while $show_menu1 ; do

# Setup Menu string Fields displayed for your menu.  Set quotes all equal to longest line text

  MENU="Main Selection Menu"
  TOTL="9"
  TSK1=" 1. Menu Option Selection 1"
  TSK2=" 2. Menu Option Selection 2"  
  TSK3=" 3. Menu Option Selection 3"
  TSK4=" 4. Menu Option Selection 4"
  TSK5=" 5. Menu Option Selection 5" 
  TSK6=" 6. Menu Option Selection 6"
  TSK7=" 7. Menu Option Selection 7"  
  TSK8=" 8. Menu Option Selection 8"
  TSK9=" 9. Menu Option Selection 9"  

# Call Menu and get user input selection

  print_menu "$TITLE" "$MENU" "$TOTL" "$MyVersion" "$TSK1" "$TSK2" "$TSK3" "$TSK4" "$TSK5" "$TSK6" "$TSK7" "$TSK8" "$TSK9"
  CHOICE1="$?"
  color 7 0 N

# *****************************************************************************
#                           Execute User Command Here 
# *****************************************************************************

  case "$CHOICE1" in
    "1")  ;;
    "2")  ;;
    "3")  ;;
    "4")  ;;
    "5")  ;;    
    "6")  ;;
    "7")  ;;         
    "8")  ;;
    "9")  ;;
    "0")  show_menu1=false ;;
    *) ;;
  esac

done 

show_gpl 

exit 0

# End Of Script

Save it as the file menudemo in your $HOME/bin folder (/home/username/bin). Then open up terminal and run thius command to make it executable:

chmod +x $HOME/bin/menudemo

To see it work, just type in:

menudemo

Once you get the hang of it, just add in your own command and menu titles. Make surer and modify the author name in the script to be yours and not mine, becuase once you make a change, it is all yours.

Thank You,**