Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 28
Where do I learn Bash Programming from ? Any source where I can download source code or script to learn ? I'm reading "Learning Bash" , but its not helping ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18

    Bash Programming


    Where do I learn Bash Programming from ?

    Any source where I can download source code or script to learn ?

    I'm reading "Learning Bash" , but its not helping me

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,442
    Hi and welcome

    Is this what you are reading?
    Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)): Cameron Newham: 9780596009656: Amazon.com: Books

    In what area or specific case do you need help?
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Okay, I'm looking @ 1 file.

    shell file
    Code:
    #display main ticketing menu
    display_menu()
    {
      # Called by run_menu
      echo
      echo "-- Theatre Self-service Ticketing --"
      echo -n "1. "
      echo -e '\E[1;32;40mList all Movies and Show time\E[0m'
      echo -n "2. "
      echo -e '\E[1;32;40mDisplay theatre鈥檚 seat \E[0m'
      echo -n "q. "
      echo -e '\E[1;31;47mQuit\E[0m'
    	
      echo -en "Enter your selection: "
    }
    
    # List out available movies
    list_movies()
    {
    	# Called by run_menu
    	
    	old_IFS=$IFS 
    	IFS=$'\n'
    	print_movie_header
    	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt`  
    	do
    		print_movie $LINE
    	done
      IFS=$old_IFS 
    }
    	
    # Output header for movie listing
    # Catered for both numbered and non-numbered list
    print_movie_header()
    {
    	echo -e '\E[1;32;40m'
    	if [ $# == "1" ];then
    		echo -n "   "
    	fi
    	echo -n -e 'Movie Title'
      set_colwidth 'Movie Title' 25
    	echo -n -e 'Show Time\t'
    	if [ $# == "1" ];then
    		echo -n -e "\t"
    	fi
    	echo -n 'Vacancy'
    	echo -e '\E[0m'
    }
    
    # Set the width of column
    # $1 is the text in the column 
    # and $2 is the intended column width
    set_colwidth()
    {
    	STRING=$1
    	LENGTH=${#STRING}
    
    	for (( i=$LENGTH; i<=$2; i++))
    	do 
    		echo -n " "
    	done 
    
    }
    
    # Output movie entry
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
      echo $AVAILABLE
    }
    
    
    # Hint2
    # Get current seating arrangement of 
    # selected movie from seating.txt
    movie_data()
    {
    	COUNT=1
    	old_IFS=$IFS 
    	IFS=$'\n'
    	for LINE in `cat seating.txt`  
    	do
    		if test $1 -eq $COUNT
    			then
    				display_seats $LINE $2
    
    		fi
    		THEATER[$COUNT]=$LINE
    		let COUNT=COUNT+1
    		
    	done
      IFS=$old_IFS 	
    }
    
    # Mark occupied seat with a X and
    # seats selected by user with a red X
    mark_seat()
    {
    	# Called by display_seats
    	
    	if [ $(echo "$1" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -n "X" 
    		  set_colwidth "" 5
    	elif [ $(echo "$4" | grep -c "$2$3") -gt 0 ]
    	then
    			echo -en '\E[1;31mX\E[0m'
    		  set_colwidth "" 5
    	else
    			set_colwidth "" 6 
    	fi
    }
    
    # Print out theater's seating arrangement
    display_seats()
    {
    	COL=5
    	ROW=4
    	echo
    
    	echo "---------------------------------------"
    	echo -e '\E[1;34;47m----------------SCREEN-----------------\E[0m'
    	echo "---------------------------------------"
    	for (( x=0; x<=$ROW; x++))
    	do 
    	echo -n "$x" | tr "0" " "
    		for (( j=0; j<=$COL; j++))
    		do 
    			if [ $x == "0" ]
    			then
    				echo -n $j | tr "012345" " ABCDE"
    				set_colwidth "" 5
    			else
    				CURR_COL=$(echo $j | tr "12345" "ABCDE")
    				if [ $j == "0" ]
    				then
    					set_colwidth "" 6
    				else
    					mark_seat $1 $CURR_COL $x $2
    				fi
    			fi
    	done 
      echo ""
    	done
    	echo "---------------------------------------"
    }
    
    
    # Select and book seat manually
    select_seat()
    {
    	# Called by run_menu
    	
    	COUNT=0
    	echo
    	echo "-- Select A Seat Manually --"
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	print_movie_header "numbered"
    	for LINE in `sed -e 's/Yes/Ticket Available/g' -e 's/No/SOLD OUT/g' movielist.txt `;do
    		let COUNT=COUNT+1
    		echo -n "$COUNT. "
    		print_movie $LINE
    	done
    	IFS=$old_IFS 
    	echo
    	
    	# Select movie to book
    	echo -en "Select Movie To Book (Enter 1 to ${COUNT}): "
    	read NUM
    	NUMERIC=0
    	while [ "$NUMERIC" != "1" ];do
    		for (( x=$COUNT; x>=1; x--))
    		do
    			if [ $NUM == "$x" ];then
    				NUMERIC=1
    			fi
    		done
    
    		if [ "$NUMERIC" != "1" ];then
    			echo -en "Invalid Movie Selected, Try Again (Enter 1 to ${COUNT}): "
    			read NUM
    		fi
    	done
    
    	movie_data $NUM
    	echo
    	old_IFS=$IFS 
    	IFS=$'\n'
    	
    	 
    	echo
    }
    
    # Main menu navigation
    run_menu()
    {
    	i=-1
      while [ "$i" != "q" ]; do
    		display_menu
    		read i
    		i=`echo $i | tr '[A-Z]' '[a-z]'`
    
    		case "$i" in 
    	  	"1")
    			list_movies
    			;;
    		"2")
    			select_seat
    			;;
    		"q")
    			echo "GoodBye, Have A Nice Day"
    			exit 0
    			;;
    		*)
    			echo "Unrecognised Input."
    			;;
    		esac
      done
    }
    
    
    #----------------------------------------------#
    # ---------PROGRAM SCRIPT START HERE---------- #
    #----------------------------------------------#
    
    if [ ! -f movielist.txt ]; then
      echo "Error: Movie list is not available"
    fi
    
    
    run_menu
    movielist.txt
    Code:
    1300 – 2Oct,The Social Network,No
    1300 – 2Oct,The Town,Yes
    1700 – 2Oct,Paranormal Activity 2,Yes
    1900 – 2Oct,You Again,Yes
    2100 – 2Oct,Kamui,Yes
    seating.txt
    Code:
    0;,A2,A3,A1,A7,A9
    0;,E6,E7,E8
    
    14;,A1,D4,C3,B3,A4,E4
    15;,A1,D4,E2,C3,A2
    15;,A1,B2,C3,D4,A4
    Can anyone explain to me the shell ?

    How do you create extra table in the heading

    Movie Title Time Slot Availability (how to include Book Now )

    How do you also align the data to fit the header

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    anyone or any book who can teach me how to use bash/

  6. #5
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238

  7. #6
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Can anyone willing to teach me , online tutoring ?

  8. #7
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by uglyboy View Post
    Can anyone explain to me the shell ?
    i believe that is beyond the constraints of a forum. You've got read what you can, absorb it, and apply it to the code that you attempt. Follow tutorials and perform the examples they provide. You will get it, just stick with it.

    How do you create extra table in the heading

    How do you also align the data to fit the header
    For these types of formatting issues, you ought to use printf. It is a Bash shell built-in function, and possibly also a more capable binary (/bin/printf). Here's an example:
    Code:
    #!/bin/bash
    menus=(colA colB colC colD)
    values=('foo' 'bar' 'really long value' 'meh')
    
    printf "%-8s"   ${menus[0]}
    printf "%-8s"   ${menus[1]}
    printf "%-20s"  "${menus[2]}"
    printf "%s\n" ${menus[3]}
    
    printf "%-8s"   ${values[0]}
    printf "%-8s"   ${values[1]}
    printf "%-20s"  "${values[2]}"
    printf "%s\n" ${values[3]}
    The "%s" is simply the string you are printing. The "%-8s" means to left justify text, to the 8th character. Similar with %-20s. You can also right justify, and a bunch of other stuff with printf.
    Last edited by atreyu; 11-20-2012 at 04:26 AM. Reason: typo

  9. #8
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Code:
    print_movie_header()
    {
    	echo -e '\E[1;32;40m'     #colour highlight
    	if [ $# == "1" ];then
    		echo -n "   "
    	fi
    	echo -n -e 'Movie Title'          #movie title appears with the length of 25 char
      set_colwidth 'Movie Title' 25
    	echo -n -e 'Show Time\t'
    	if [ $# == "1" ];then
    		echo -n -e "\t"
    	fi
    	echo -n 'Vacancy'              #vacancy
    	echo -e '\E[0m'                   #end of highlight
    }
    
    # Set the width of column
    # $1 is the text in the column 
    # and $2 is the intended column width
    set_colwidth()
    {
    	STRING=$1
    	LENGTH=${#STRING}
    
    	for (( i=$LENGTH; i<=$2; i++))           #S2 refers to ??? Let us say I want more values
    	do 
    		echo -n " "
    	done 
    
    }
    
    # Output movie entry
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
      echo $AVAILABLE
    }

    Right now, it shows


    Movie Title Timing Availability
    Bla Bla -------------- 1500 ---------- YES / Sold Out #because of the sed function

    It happens like that when I try to have 4 information under header
    Header1 ------------Header2-----------Header3-----------Header4
    Bla1------------- ---- Bla 2 ---------------Bla3
    Bla4

  10. #9
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Just look where there's only something declared 3 times instead of 4.

    Code:
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
      echo $AVAILABLE
    }
    See, you're only cutting 3 headings instead of 4.

  11. #10
    Just Joined!
    Join Date
    Nov 2012
    Posts
    18
    Quote Originally Posted by Cancerous View Post
    Just look where there's only something declared 3 times instead of 4.

    Code:
    print_movie()
    {
    	TIME=`echo $1 | cut -f1 -d ","`
    	TITLE=`echo $1 | cut -f2 -d ","`
    	AVAILABLE=`echo $1 | cut -f3 -d ","` 
               #*   BOOKNOW =`echo $1 | cut -f4 -d ","`
    
    	echo -n -e $TITLE
    	set_colwidth $TITLE 25
    	echo -n -e $TIME '\t'
                    echo $AVAILABLE
    #* echo -n -e $BOOKNOW
    
    }
    See, you're only cutting 3 headings instead of 4.
    This isn't the one I did with the editing, I'm saying can I just do this.

    It displays Time First instead of Title first, I want to know why

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •