LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   tab space issue in script (https://www.linuxquestions.org/questions/linux-newbie-8/tab-space-issue-in-script-4175577684/)

arun natarajan 04-17-2016 08:36 PM

tab space issue in script
 
hi,

output format is getting different one with wrong tab space alignment. even i ve checked with the tab spaces. need solution for the same.

[root@server ~]# cat variables
#!/bin/bash
echo "File Name : $0"
echo "First Argument : $1"
echo "Second Argument : $2"
echo "Quoted Values : $*"
echo "Total Number of Arguments : $#"
echo "PID of the scipt : $$"
echo "PID of last background process : $!"

[root@server ~]# sh variables arun akash
File Name : variables
First Argument : arun
Second Argument : akash
Quoted Values : arun akash
Total Number of Arguments : 2
PID of the scipt : 3802
PID of last background process :
[root@server ~]#

rknichols 04-17-2016 09:34 PM

Since posting without using [CODE] ... [/CODE] tags did not preserve your tabs, it's a bit hard to see what you are trying to do. I presume you want those arguments all aligned regardless of the length of the label. Doing that with echo is going to be difficult. Use printf:
Code:

#!/bin/bash
printf "%-32s %s\n" "File Name :" "$0"
printf "%-32s %s\n" "First Argument :" "$1"
printf "%-32s %s\n" "Second Argument :" "$2"
printf "%-32s %s\n" "Quoted Values :" "$*"
printf "%-32s %s\n" "Total Number of Arguments :" "$#"
printf "%-32s %s\n" "PID of the scipt :" "$$"
printf "%-32s %s\n" "PID of last background process :" "$!"


syg00 04-17-2016 10:28 PM

or "man tabs"

grail 04-17-2016 11:02 PM

If you wanted to do it by hand I would do:
Code:

#!/usr/bin/env bash

cat <<-EOF
  File Name                          : $0
  First Argument                  : $1
  Second Argument                  : $2
  Quoted Values                          : $*
  Total Number of Arguments          : $#
  PID of the script                  : $$
  PID of last background process  : $!
EOF



All times are GMT -5. The time now is 04:59 AM.