Thread: [Linuxcommand-discuss] shell printf function
Brought to you by:
bshotts
From: Bram M. <bra...@li...> - 2003-02-16 00:26:53
|
Hi, The signature at the end of this message is generated by two little shell-scripts I wrote: ---sig-linux--- #!/bin/bash echo '<pre>-- ' echo '# Mertens Bram "M8ram" <bra...@li...> Linux User #249103 #' /home/m8ram/bin/sig-general ---end--- ---sig-general--- #!/bin/bash current_uptime=$(uptime) current_kernel=$(uname -r) current_machine=$(uname -m) current_release="SuSE 8.1 Pro" echo -n '# '; echo -n $current_release; echo -n ' kernel '; echo -n $current_kernel; echo -n ' '; echo -n $current_machine; echo ' 128MB RAM #' echo -n '# '; echo -n $current_uptime; echo ' #' echo '</pre>' ---end--- Unfortunately it still doesn't look very pretty, what I would like is something like: ---new-sig--- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # # 1:16am up 12:19, 1 user, load average: 0.06, 0.04, 0.06 # ---end--- Is there a shell-function that allows the output to be of certain length like printf in C and Perl (IIRC)? I want all lines to be 78 chars wide, regardless of the kernel-version or the uptime. Is this possible with a shell-script? Or should I write a Perl-script? TIA -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # # 1:16am up 12:19, 1 user, load average: 0.06, 0.04, 0.06 # |
From: William S. <bs...@pa...> - 2003-02-16 02:31:14
|
Bram Mertens wrote: > Hi, > > The signature at the end of this message is generated by two little > shell-scripts I wrote: > ---sig-linux--- > #!/bin/bash > > echo '<pre>-- ' > echo '# Mertens Bram "M8ram" <bra...@li...> Linux User > #249103 #' > /home/m8ram/bin/sig-general > ---end--- > ---sig-general--- > #!/bin/bash > current_uptime=$(uptime) > current_kernel=$(uname -r) > current_machine=$(uname -m) > current_release="SuSE 8.1 Pro" > > echo -n '# '; echo -n $current_release; echo -n ' kernel '; echo -n > $current_kernel; echo -n ' '; echo -n $current_machine; echo ' 128MB RAM > #' > echo -n '# '; echo -n $current_uptime; echo ' #' > echo '</pre>' > ---end--- > > Unfortunately it still doesn't look very pretty, what I would like is > something like: > ---new-sig--- > # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # > # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # > # 1:16am up 12:19, 1 user, load average: 0.06, 0.04, 0.06 # > ---end--- > > Is there a shell-function that allows the output to be of certain length > like printf in C and Perl (IIRC)? > I want all lines to be 78 chars wide, regardless of the kernel-version > or the uptime. > > Is this possible with a shell-script? Or should I write a Perl-script? > > TIA bash contains a printf command that is mostly like the C/perl/etc function. Try "help printf" at the bash prompt. Failing that, many distributions have a printf program that can be called from scripts. Try "man printf" -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |
From: Bram M. <bra...@li...> - 2003-03-28 19:10:00
|
On Sun, 2003-02-16 at 03:30, William Shotts wrote: > Bram Mertens wrote: [snip] > > ---sig-general--- > > #!/bin/bash > > current_uptime=$(uptime) > > current_kernel=$(uname -r) > > current_machine=$(uname -m) > > current_release="SuSE 8.1 Pro" > > > > echo -n '# '; echo -n $current_release; echo -n ' kernel '; echo -n > > $current_kernel; echo -n ' '; echo -n $current_machine; echo ' 128MB RAM > > #' > > echo -n '# '; echo -n $current_uptime; echo ' #' > > echo '</pre>' > > ---end--- [snip] > bash contains a printf command that is mostly like the C/perl/etc > function. Try "help printf" at the bash prompt. Failing that, many > distributions have a printf program that can be called from scripts. > Try "man printf" I can't seem to get this working, I've tried several variations of the following but A always get the same error: m8ram@linux:~> sig-general # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # # 7:59pm up 11 days, 1:21, 6 users, load average: 2.18, 2.13, 2.10 # /home/m8ram/bin/sig-general: line 9: syntax error near unexpected token `"# %-65s #",' /home/m8ram/bin/sig-general: line 9: `printf("# %-65s #", curent_uptime)' I've tried to specify the variable as $current_uptime, $(surrent_uptime), ($current_uptime),... I've tried %c and %s, single quotes, double quotes... What is the right format? TIA -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # # 8:01pm up 11 days, 1:23, 6 users, load average: 2.59, 2.22, 2.13 # |
From: William S. <bs...@pa...> - 2003-03-28 20:05:18
|
In bash, the correct format would be: printf "# %-65s #\n" "$current_uptime" Don't use the parens. Also, you need to quote the argument since it contains embedded spaces. Lastly, you should include the "\n" in the format string since there is no carriage return if you don't explicitly specify it. ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org On Fri, 28 Mar 2003, Bram Mertens wrote: > On Sun, 2003-02-16 at 03:30, William Shotts wrote: > > Bram Mertens wrote: > [snip] > > > ---sig-general--- > > > #!/bin/bash > > > current_uptime=$(uptime) > > > current_kernel=$(uname -r) > > > current_machine=$(uname -m) > > > current_release="SuSE 8.1 Pro" > > > > > > echo -n '# '; echo -n $current_release; echo -n ' kernel '; echo -n > > > $current_kernel; echo -n ' '; echo -n $current_machine; echo ' 128MB RAM > > > #' > > > echo -n '# '; echo -n $current_uptime; echo ' #' > > > echo '</pre>' > > > ---end--- > [snip] > > bash contains a printf command that is mostly like the C/perl/etc > > function. Try "help printf" at the bash prompt. Failing that, many > > distributions have a printf program that can be called from scripts. > > Try "man printf" > > I can't seem to get this working, I've tried several variations of the > following but A always get the same error: > m8ram@linux:~> sig-general > # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # > # 7:59pm up 11 days, 1:21, 6 users, load average: 2.18, 2.13, 2.10 # > /home/m8ram/bin/sig-general: line 9: syntax error near unexpected token > `"# %-65s #",' > /home/m8ram/bin/sig-general: line 9: `printf("# %-65s #", > curent_uptime)' > > I've tried to specify the variable as $current_uptime, > $(surrent_uptime), ($current_uptime),... > I've tried %c and %s, single quotes, double quotes... > > What is the right format? > > TIA > -- > # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # > # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # > # 8:01pm up 11 days, 1:23, 6 users, load average: 2.59, 2.22, 2.13 # > > > > ------------------------------------------------------- > This SF.net email is sponsored by: > The Definitive IT and Networking Event. Be There! > NetWorld+Interop Las Vegas 2003 -- Register today! > http://ads.sourceforge.net/cgi-bin/redirect.pl?keyn0001en > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss > |
From: Bram M. <bra...@li...> - 2003-03-30 18:39:02
|
On Fri, 2003-03-28 at 21:05, William Shotts wrote: > In bash, the correct format would be: > > printf "# %-65s #\n" "$current_uptime" > > Don't use the parens. Also, you need to quote the argument since it > contains embedded spaces. Lastly, you should include the "\n" in the > format string since there is no carriage return if you don't explicitly > specify it. Thanks! Here's the script with some more changes: #!/bin/bash current_uptime=$(uptime) current_kernel=$(uname -r) current_machine=$(uname -m) current_release="SuSE 8.1 Pro" mem="128MB" NAME="Mertens Bram \"M8ram\"" EMAIL=$1 LINUXID="Linux User #249103" printf "<pre>-- \n" printf "# %-22s %-29s %22s #\n" "$NAME" "$EMAIL" "$LINUXID" printf "# %-16s kernel %-15s %-6s %18s RAM #\n" "$current_release" "$current_kernel" "$current_machine" "$mem" printf "#%70s #\n" "$current_uptime" printf "</pre>\n" I have two other shell scripts that call this script to provide the correct e-mail address, they only contain a single line like: sig-general "<bra...@li...>" Where sig-general is the name of the above script. The reason I need this is because Evolution (my MUA) can call scripts to generate sigs but it can't pass arguments to those scripts (yet). You can see the result below, thanks again for the help! Regards -- # Mertens Bram "M8ram" <bra...@li...> Linux User #249103 # # SuSE 8.1 Pro kernel 2.4.19-4GB i686 128MB RAM # # 10:53am up 11 days, 16:15, 6 users, load average: 2.19, 2.10, 2.05 # |