Visit Our UNIX and Linux User Community


Bash Info Display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Info Display
# 1  
Old 09-19-2012
Bash Info Display

I have written the following bash function prArgv

Suppose the calling sequence is as follows

Code:
prArgv VAL1 VAL2 DESC VAL3 VAL4 v2d1 s4 p15

The call will look at the tag k1v2, add the numbers together, in this case 2+1=3
This means that the function will look at the first 3 user arguments only (VAL1 VAL2 DESC).

The s4 tag is a shift tag, so that when I print VAL1 VAL2 DESC, everything is shifted by 4 characters
to the right.

Thus

Code:
1234VAL1 VAL2 DESC     # print statement shifted by 4 characters to the right.

The thing I am trying to do is that if the user puts the p-tag, the description which is the
last user argument defined ($3), is printed at the position defined by the corresponding number.

For example, p15 will place DESC starting from the 15 character position.


Code:
prArgv() {

  frmt_v2d1="${bGn}%s %s${nWh}    %s${nColor}\n"

  #=============================================================================
  # Set location of format tag and the shift
  lastArgv="$#"  # lastArgv =

  # Position of last argument equals the number of arguments
  frmtPos=`expr $lastArgv - 1`  # frmtPos =

  # Position of argument before last
  shiftpos=$lastArgv  

  #=============================================================================
  # Construct variable names from where to capture the required format
  # Construct shift format variable, 's' followed by amount of shift
  frmtShift="${!shiftpos}"  # frmtShift = "s2"

  # Construct arguments format variable for colored output
  frmtArgv="frmt_${!frmtPos}"
 
  #=============================================================================
  # Construct print format
  # Shift tag detected

  if [[ "${!shiftpos}" =~ ^s[0-9]+$ ]] ; then
      frmt="${!frmtShift}${!frmtArgv}"

  # Position tag detected
  elif [[ "${!shiftpos}" =~ ^p[0-9]+$ ]] ; then
      frmt="${!frmtShift}${!frmtArgv}"

  # No shift or position tags detected
  else
      frmt="${!frmtArgv}"
  fi
  echo "frmt = $frmt"

  #=============================================================================
  #  Compute number of arguments to print. This is done by adding the numbers
  # in the format tag together.
  sepString=`echo "${!frmtPos}" | sed 's/\([0-9][0-9.]*\)/ & /g'`
  strArr=( $sepString )
  
  # Add the numeric fields together
  n=0
  for str in "${strArr[@]}"; do
     if [[ "$str" =~ ^[0-9]+$ ]]; then
       n=$((n+str))
     fi
  done
   
  #=============================================================================
  # Compute the total length of all user arguments excluding the description
  m=$((n-1))
  strArr=( ${@:1:$m} )
  echo "str = ${@:1:$m}"
  totlLen=0
  for str in "${strArr[@]}"; do
    lenArgv=${#str}
    totlLen=$((totlLen+lenArgv))
  done
  echo "totlLen = $totlLen"
  #  str=${!n}
  #  strLen=${#str}
  #  echo "str, strLen = $str, $strLen"
  printf "$frmt" "${@:1:$n}"  # print command
}


Last edited by kristinu; 09-19-2012 at 01:05 PM..
# 2  
Old 09-19-2012
I am lost -- it is not all described, and yet other stuff is in there. What is the question? How to pad a field to a specific length? Can we assume it fits? Right or left? Just add one space at a time until the lenght is right?

Previous Thread | Next Thread
Test Your Knowledge in Computers #757
Difficulty: Medium
The MOSFET is the least common semiconductor device in digital and analog circuits.
True or False?

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Display info about users (UID GID processes terminal)

I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made. 1. The problem statement, all variables and given/known data: The task is to create a script which prints information about users whose names are specified in the... (2 Replies)
Discussion started by: kornfan
2 Replies

2. Shell Programming and Scripting

Search info about awk bash instruction

hello i have search in an old .bash_history and i find this instruction : xhost `xhost | awk -F: 'NR > 1 {printf ("-%s ", $NF)}'` someone give me some informations about this instruction?:confused: thx (2 Replies)
Discussion started by: overflow
2 Replies

3. Solaris

Bash display error

Some of my admin made some changes on my Solaris-10 box and after that I started getting this wiered issue. I checked path, but not able to figure it out. This is for a non-root user gcadmin@brbpod06: $ echo $SHELL /usr/bin/bash gcadmin@brbpod06: $ bash bash: brbpod06:: command not found... (2 Replies)
Discussion started by: solaris_1977
2 Replies

4. Shell Programming and Scripting

Event logging to file and display to console | tee command is not able to log all info.

My intention is to log the output to a file as well as it should be displayed on the console > I have used tee ( tee -a ${filename} ) command for this purpose. This is working as expected for first few outputs, after some event loggin nothing is gettting logged in to the file but It is displaying... (3 Replies)
Discussion started by: sanoop
3 Replies

5. UNIX for Advanced & Expert Users

Unable to display directory info with ps command

Hello, I start an adapter using the following command - nohup ./start_embargoAdapter >/dev/null 2>&1 & and when I do the following, I can see: /export/home/xxxxx> ps -ef | grep embargo xxxxx 28086 20761 0 23:23:29 pts/7 0:00 grep embargo xxxxx 8866 1 0 Oct 06 ? 0:00... (2 Replies)
Discussion started by: samjna
2 Replies

6. Shell Programming and Scripting

Display output bash program

Hello, i have a problem with the output from my bash program. I made this program #!/bin/bash BESTANDEN=$* # Plaatst bestanden in de variabele BESTANDEN TMPFILE=xmlprog.sh.$$.$RANDOM # basisnaam voor tijdelijke bestanden # controller of het programma correct is aangeroepen if then ... (6 Replies)
Discussion started by: dutchspiders
6 Replies

7. Shell Programming and Scripting

Run a bash script, display on the screen and save all information in a file including error info

Hi all, How to: Run a bash script, display on the screen and save all information in a file including error information. For example: I have a bash script called test.sh now I want to run the test.sh and display the output on the screen and save the output including error info to a file. ... (1 Reply)
Discussion started by: Damon sine
1 Replies

8. Shell Programming and Scripting

Kill -9 within Bash script kicks out usage info

I have a start|stop|restart script for a custom app we have. After it tries to stop our process the correct way, it checks to see if it's gone, if not it tries to kill it, if that doesn't work kill -9. If I run kill -9 on the PID from the command line it kills it and all is well. If I have the... (1 Reply)
Discussion started by: mglenney
1 Replies

9. Programming

program or script to display user info

I'm on a Linux machine and need a program that will display user information as follows: user name, user directory and current date & time. I think we can compile C, C++ and Perl. All help is appreciated. (4 Replies)
Discussion started by: flasun
4 Replies

10. Shell Programming and Scripting

Find files older then today & display with timestamp info

Small query- I want to do some operation on all the files older then today. Before I do that operation, i want to verify if the command works properly or not. Surprisingly, the command below returns me file, which are created today - find /mrk_archive/PG/ftp/incomming/gbs/2008 -type f... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

Featured Tech Videos