Results 11 to 20 of 28
Originally Posted by uglyboy
anyone or any book who can teach me how to use bash/
"Advanced Bash-Scripting Guide" link shared by watael,is an excellent link to learn in-depth bash.
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-20-2012 #11
"Advanced Bash-Scripting Guide" link shared by watael,is an excellent link to learn in-depth bash.
But there are lot of quick tutorials online,here is one of them (which used few years back)
http://www.giis.co.in/Bash_Shell_Programming.pptFirst they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-----
FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org
- 11-20-2012 #12Just Joined!
- Join Date
- May 2012
- Posts
- 43
I don't understand...
You've assigned TIME to field one, and TITLE to field 2.It displays Time First instead of Title first, I want to know why
Just switch which field you're assigning them to in your cut commands.
Look at the cut man page as well as the echo man page.
Also, when assigning variables, instead of using backticksuseCode:variable=`command`
Backticks run in a sub-proccess which is less efficient than running them the other way.Code:variable=$(command)
- 11-20-2012 #13Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
anyone ? who is willing to give me your MSN.
Can I paste a screenshot of what I achieved and expect a response ?
- 11-20-2012 #14Just Joined!
- Join Date
- May 2012
- Posts
- 43
Absolutely. Screenshots are great!
Also use the code tags around your code in comments, it retains formatting if you copy and paste text from the shell.
- 11-20-2012 #15Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
Thanks, jst give me a while, I will post the SS, I just dont understand how do you replace this Movie Reservation to make it like the Airline Reservation.
I believe concept wise ,its the same.
Retrieving of the movielist / airlineinfo is from txt, so you can't use the printf method
- 11-20-2012 #16Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
Code:#display main ticketing menu display_menu() { # Called by run_menu echo echo "-- Theatres 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 theatres 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 ","` #CHECK=`echo $1 | cut -f4 -d" ","` echo -n -e $TITLE set_colwidth $TITLE 25 echo -n -e $TIME '\t' echo $AVAILABLE #echo -n -e $CHECK } # 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
Code:1300 2Oct,The Social Network,No, 1300 2Oct,The Town,No, 1700 2Oct,Paranormal Activity 2,Yes, 1900 2Oct,You Again,Yes, 2100 2Oct,Kamui,Yes,
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
- 11-20-2012 #17Just Joined!
- Join Date
- May 2012
- Posts
- 43
What do you mean? Are you just trying to change the values so it looks like an airline timetable rather than a movie value?
You also need to show us the output you're getting currently.
All the commands are pretty basic in this script, if you're just trying to understand it look at the man pages.
- 11-20-2012 #18Just Joined!
- Join Date
- Nov 2012
- Posts
- 18
blabla(ww.i6.minus.com/jDAuipsHCK41d.png
Code:#display main ticketing menu display_menu() { # Called by run_menu echo echo "-- Theatre’s 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’s 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' set_colwidth 'Vacancy' 12 echo -n 'Cinema Showing' 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 ","` CHECK=`echo $1 | cut -f4 -d ""` echo -n -e $TITLE set_colwidth $TITLE 25 echo -n -e $TIME '\t' echo $AVAILABLE echo $CHECK } # 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_menuCode:1300 – 2Oct,The Social Network,No,Lido 1300 – 2Oct,The Town,No,Shaw, 1700 – 2Oct,Paranormal Activity 2,Yes,Cathay, 1900 – 2Oct,You Again,Yes,Lido, 2100 – 2Oct,Kamui,Yes,Shaw,
I want the result to be
Movie Title---------------ShowTime--------------Vacancy--------------Theatre Showing
Mission Impossible--------1500-1600-------------AVAILABLE ------------ Filmgarde
- 11-21-2012 #19Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,695
again, i would use printf for this - much cleaner. also, provided you're using a recent version of Bash (and are specifying it at the top of the script, as in #!/bin/bash, and not #!/bin/sh), i'd recommend not mucking with IFS (for the same reasons) when there are other ways to do it, e.g.:
diddle with the numbers (26, 14, etc.) in the printf format to modify the justification of the columns.Code: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 #echo "printing movie line '$LINE'" # print_movie $LINE # done # IFS=$old_IFS while read line; do date=$(echo "$line"|awk -F, '{print $1}') title=$(echo "$line"|awk -F, '{print $2}') avail=$(echo "$line"|awk -F, '{print $3}') cine=$(echo "$line"|awk -F, '{print $4}') printf "%-26s%-14s%-13s%s\n" "$title" "$date" "$avail" "$cine" done < <(cat movielist.txt) }
- 11-21-2012 #20Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi.
This demonstrates automatic alignment of columns with perl script align on a surgically altered version of the script snippets:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate perl script "align". # See: http://freecode.com/projects/align # Utility functions: print-as-echo, print-line-with-visual-space, debug. # export PATH="/usr/local/bin:/usr/bin:/bin" pe() { for _i;do printf "%s" "$_i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; } db() { : ; } C=$HOME/bin/context && [ -f $C ] && $C align 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\t' # set_colwidth 'Vacancy' 12 # echo -n 'Cinema Showing' # echo -e '\E[0m' echo -e "Movie Title\tShow Time\tVacancy\tCinema SHowing" } 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 #echo "printing movie line '$LINE'" # print_movie $LINE # done # IFS=$old_IFS while read line; do date=$(echo "$line"|awk -F, '{print $1}') title=$(echo "$line"|awk -F, '{print $2}') avail=$(echo "$line"|awk -F, '{print $3}') cine=$(echo "$line"|awk -F, '{print $4}') # printf "%-26s%-14s%-13s%s\n" "$title" "$date" "$avail" "$cine" printf "%s\t" "$title" "$date" "$avail" "$cine" printf "\n" done < <(cat movielist.txt) } FILE=${1-data1} pl " Input data file $FILE:" cat $FILE cp $FILE movielist.txt pl " Results:" list_movies | tee f1 | align -st exit 0
So to summarize, place a TAB between all fields of importance and run through align -st. See file f1 for raw input to align in this script.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) bash GNU bash 3.2.39 align 1.7.0 ----- Input data file data1: 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 ----- Results: Movie Title Show Time Vacancy Cinema SHowing The Social Network 1300 2Oct No The Town 1300 2Oct Yes Paranormal Activity 2 1700 2Oct Yes You Again 1900 2Oct Yes Kamui 2100 2Oct Yes
See comment in scipt for link to align source.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )



Reply With Quote
