LinuxQuestions.org
Help answer threads with 0 replies.
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 08-02-2013, 05:18 AM   #1
sigint-ninja
Member
 
Registered: Feb 2011
Location: Republic Of Ireland
Distribution: Debian,Centos,Slackware
Posts: 508

Rep: Reputation: 29
Can somebody answer a BASH question


Bash scripting question...
Hi guys,

i know what bash is...and i know what scripting is

but where is bash scripting used mostly...i mean can somebody give me a real world example
of how bash scripting is used in day to day working environments

and is there good work placement for a guy who knows bash scripting well...
 
Old 08-02-2013, 05:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by sigint-ninja View Post
but where is bash scripting used mostly...i mean can somebody give me a real world example
of how bash scripting is used in day to day working environments
Bash scripts (and one-liners) are used all over the place:

- init scripts,
- create backups,
- statistics collection,
- parsers,
- cleaners,
- extractors,
- installers,
- etc,etc,etc.

Quote:
and is there good work placement for a guy who knows bash scripting well...
Knowing bash (scripting) is just one tool that is good to have. If bash scripting is the only thing you are good at then the chances of getting work will not increase.
 
Old 08-02-2013, 05:50 AM   #3
dt64
Member
 
Registered: Sep 2012
Distribution: RHEL5/6, CentOS5/6
Posts: 218

Rep: Reputation: 38
Just a few examples:

-When users log in a few files are executed, e.g. .bashrc, .bash-profile etc
-Cronjobs
-Loads of (semi-)automated tasks, e.g. backups
-Things you need to do more than once
-system handling, e.g. ifup/ifdown etc.
-service/deamon handling, e.g. /etc/init.d files

edit: too late
 
Old 08-02-2013, 10:04 AM   #4
David Trest
Member
 
Registered: Jul 2013
Distribution: CentOS/RHEL, Backtrack, many more.
Posts: 58

Rep: Reputation: Disabled
Sysadmins (like myself) have built scripts to automate tasks and make things more user-friendly.

At a previous posting, I designed a backup solution that used rman, gpg, and tar to write to an attached tape drive. Rather than teaching the rest of the staff who would be running the backups (as most of them didn't have the UNIX skills I do), I wrote a shell wrapper for it that would run the tasks and present them with output so they were aware of what was going on.
 
Old 08-02-2013, 10:10 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by sigint-ninja View Post
Bash scripting question...
Hi guys,

i know what bash is...and i know what scripting is

but where is bash scripting used mostly...i mean can somebody give me a real world example
of how bash scripting is used in day to day working environments

and is there good work placement for a guy who knows bash scripting well...
Bashing is for automation mainly. You make a bash script for anything you end up doing constantly. Like backups.

My rule as a Network admin is, if I do it manually more than once, I will script it so I can do it in less time, but pretend it toook the same amount of time and go to the bar.
 
Old 08-02-2013, 01:19 PM   #6
Ranamon
Member
 
Registered: Feb 2013
Location: Land of Hopenchange
Distribution: Slackware
Posts: 45

Rep: Reputation: Disabled
Quote:
Originally Posted by sigint-ninja View Post
but where is bash scripting used mostly...i mean can somebody give me a real world example of how bash scripting is used in day to day working environments
I often use it for bulk file manipulation. Here's an example:

Code:
for File in $(ls)
do
Name=$(expr $File : '\(.*\)\.wav')
sox $File $Name.cdr
done
This processes a bunch of *.wav files in a directory into *.cdr files in preparation for CD burning with cdrecord. I just type the code in at the command prompt, but it could just as easily be made into an actual BASH script. Just add the line:

"#!/bin/bash" as the first line, and

"exit 0" as the last line and you're good to go. Just don't forget to make it an executable (or start it with: "/bin/bash MyScript.sh")

Here are a couple more examples:

Code:
# Define a couple of macros to help end users
# determine if they need to install the FOX libs.
#
# Try to compile a very simple FOX app that calls
# the default constructor of the FXApp object, and
# immediately throws it away. If this object can't be
# called, a FOX app can't run.

AC_DEFUN([SIMO_FOX_COMPILE],
[# FOX Version: 1.6

ac_fox_compile="$CXX -g -c FoxTest.cpp"
cat > FoxTest.cpp << EOF
#include <fox-1.6/fx.h>
int main(void)
{
FXApp();
return 0;
}
EOF

# Compile test: must have FOX headers for this. Use system path
# to FOX header files.

if { (eval echo "$as_me:$LINENO: \"$FoxCompile\"") >&5
(eval "$ac_fox_compile") 2>&5
ac_status=$?
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(eval echo "$as_me:$LINENO: Run FoxTest") >&5
(exit $ac_status); }
then
AC_MSG_RESULT([====> Compile test successful <====])
else
rm -f FoxTest.cpp
cat << EOF

!!!!!!!!!!!!!!!! Configure Error !!!!!!!!!!!!!!!!!!!!!!
!!                                                   !!
!!  FOX libs not found on this system                !!
!!  Please install FOX from the included "tarball"   !!
!!  Then re-run configure. (See "INSTALL" for more   !!
!!  details.)                                        !!
!!                                                   !!
!!!!!!!!!!!!!!!! Configure Error !!!!!!!!!!!!!!!!!!!!!!

EOF
exit 1
fi])
AC_DEFUN is an autoconf macro that allows you to write your own m4, autoconf compatible, macros that can be dropped right into a configure.ac file. Autoconf takes that file, and working with automake, creates your configure script. When it sees "SIMO_FOX_COMPILE", it drops that bash code into the script.

Another one:

Code:
# Macro to link and run a very simplistic FOX app.
# If this test fails, it means that either the FOX
# shared libs are corrupt, missing, or most likely
# ld can't find them because they're either in some
# wierd location, or weren't registered with ld.
#
# After installing FOX, run /sbin/ldconfig to register
# the new libs. (Some distros (Slackware) do this on
# reboot, others do not.) If this test succeeds, you're
# good to go!

AC_DEFUN([SIMO_FOX_RUN],
[ac_fox_link="$CXX -lFOX-1.6 -o FoxTest FoxTest.o"
rm -f FoxTest.cpp
if { (eval echo "$as_me:$LINENO: \"$FoxLink\"") >&5
(eval "$ac_fox_link") 2>&5
ac_status=$?
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(./FoxTest) 2>&5
ac_status=$?
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); }
then
AC_MSG_RESULT([====> Linked and ran test program successfully <====])
FoxLibs="-lFOX-1.6"
else
cat << EOF

!!!!!!!!!!!!!!!! Configure Error !!!!!!!!!!!!!!!!!!!!!!
!!                                                   !!
!!  Link test failed. FOX libs are present, however, !!
!!  they were likely not registered with ld after    !!
!!  doing the install. See the file INSTALL for      !!
!!  details on installing FOX and registering these  !!
!!  libs so that ld can find them without specifying !!
!!  a path to the FOX libs. (Essential in order to   !!
!!  easily launch the app!)                          !!
!!                                                   !!
!!!!!!!!!!!!!!!! Configure Error !!!!!!!!!!!!!!!!!!!!!!
EOF

rm -f FoxTest.o
exit 1
fi

rm -f FoxTest.o
rm -f FoxTest

AC_SUBST(FoxLibs)])
This one tries to actually run the simple program. If it succeeds, then it knows the FOX libs are there and linkable. The makefile variable "FoxLibs" is set in the makefiles. It does the clean-up, and lets the configure process complete to generate the required makefiles that compile the app and install it.

If it blows up, configure exits on an error.

You can define all sorts of custom tests for configure with a bit of BASH scripting, distribute the custom macros in a directory usually called "m4" in the distribution. You can call it whatever you like, but "m4" is a directory that aclocal looks for automatically when it finds nonstandard macros, so you don't need an extra command line switch to make it look in odd locations for undefined macros.

Quote:
and is there good work placement for a guy who knows bash scripting well...
Can't hurt if you do know it. Another thing BASH scripting is good for is quick prototyping of routines that will make their way into compiled apps, as you know right away where a script blows up, and all you need to do is edit it and run. No waiting for anything to compile to see if you've caught all the bugs.
 
Old 08-02-2013, 01:25 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
...but pretend it toook the same amount of time and go to the bar.
Work at Hooters, do ya?
 
Old 08-04-2013, 08:41 PM   #8
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
These are the 3 scripts I use the most. I some times get 50 or so pictures from club mmembers about fishing trip.

I can produce an 'html' file to put on the web site in about 15 minutes or less.

Rename the image incase of camera duplicates.
Code:
#! /bin/bash

# check if there is no command line argument
if [ $# -eq 0 ]
then
echo "You forgot the image name to be used!"
echo "Usage :: new image name"
exit
fi

new_image_name=$1_`date +"%F_%k"`
counter=1
for i in *.jpg *.JPG *.png *.gif *.jpeg; do echo "$i"; cp "$i" "$counter"_"$new_image_name".jpg; echo "$counter"_"$new_image_name" echo $counter; let "counter += 1"; done
Resize the images.
Code:
#! /bin/bash
for i in *.jpg *.JPG *.png *.gif *.jpeg; do echo "$i"; convert -size 256x "$i" -resize 256x "$i"; done
Build a table to copy past into an 'html' header.
Code:
#!/bin/bash

target=html`date +"%F_%k"` ##file name

bodyheader="
<body bgcolor=\"#efb321\" text=\"#f8c99f\" link=\"#006666\" vlink=\"#003300\">"

echo "$bodyheader" >> "$target"

headerline="
<p align=\"center\" style=\"line-height: 100%; margin-top: 0; margin-bottom: 0\">&nbsp;</p>
<table border=\"1\" width=\"90%\" bordercolor=\"#f8c99f\" bordercolorlight=\"#efb321\" bordercolordark=\"#f8c99f\" bgcolor=\"#a95205\" id=\"table1\">"

echo  "$headerline" >> "$target"


for filename in *.gif *.jpg  *.JPG *.png *.jpeg;  ##list all images in directory

  do
  if [ -f "$filename" ]
    then
### If I can get them  to rename the file this will put a caption line under the pix
### otherwise it puts the camera generated name.

	 CAPTION=${filename%.*}
  
	 imageline="
<td align=\"center\"> <p style=\"margin-top: 0; margin-bottom: 0\"> <img border=\"0\" src=\"$filename\"></a></p> <!-- <p style=\"margin-top: 0; margin-bottom: 0\">"$CAPTION"</p> -->  </td>"

	
	   echo "$imageline" >> "$target"  ##write to disk.
	   
    else
        :
    fi
	   
  done
 
Old 08-04-2013, 09:06 PM   #9
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
So there's already lot of example given to you for bash scripting how it is and as Druuna said it is just a tool and a added skill you have if you know it but that individually doesn't increase any chances of getting a good job.
 
Old 08-05-2013, 10:09 AM   #10
dt64
Member
 
Registered: Sep 2012
Distribution: RHEL5/6, CentOS5/6
Posts: 218

Rep: Reputation: 38
I found this very helpful: Advanced Bash-Scripting Guide
 
  


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
Need help creating bash script that creates directories based on what you answer Profound_Darkness Linux - General 10 03-26-2013 10:34 AM
How To Answer System Questions Inside A Bash Script ? zemon Programming 4 01-24-2011 06:07 PM
Probably easy to answer Bash question helptonewbie Programming 2 08-31-2006 05:30 PM
BASH: How to answer command prompts using redirects ?? swedish_lunacy Linux - Newbie 1 07-25-2006 02:28 AM
Bash Auto Answer Kedelfor Programming 1 11-23-2005 06:15 AM

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

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