Suggestions on this script please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suggestions on this script please
# 1  
Old 06-11-2013
Suggestions on this script please

Code:
i=1
  out=""
  j=`expr 2 * $1`
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
   done
  while [ $i -lt $j ]
  do
  print ${out%?}
  ((i=i+1))
  done

This script is throwing an error:

Code:
gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3
expr: 0402-050 Syntax error.
#
##
###
while1[10]: test: 0403-004 Specify a parameter with this command.
gurnish:/home/fnb/gurnish/saurabh/scripts>

Expected O/P:
Code:
#
##
###
##
#

Plz Advise !!!

Last edited by Scott; 06-11-2013 at 11:41 AM.. Reason: Use code tags, and a descriptive subject title
# 2  
Old 06-11-2013
I dont see how your script would produce what you want since out isn changed in second loop...
The second error refers to while with j... your expr isnt correct:j=`expr 2*$1` so in fact there seem to be just that error...
# 3  
Old 06-11-2013
Quote:
Originally Posted by vbe
I dont see how your script would produce what you want since out isn changed in second loop...
The second error refers to while with j... your expr isnt correct:j=`expr 2*$1` so in fact there seem to be just that error...
while1 3
My logic is:
First Loop:
i=1 #
i=2 ##
i=3 ###
Second Loop:
j=2 * 3 =6
for 3<6 Out as it is ###
for 4<6 Out as it is last character trimmed ##
for 5<6 Out as it is last character trimmed #

So the final O/P:

Code:
#
##
###
###
##
#

Isn't it?

Rgds,
TS

Last edited by Scrutinizer; 06-11-2013 at 03:48 PM.. Reason: code tags
# 4  
Old 06-11-2013
I do suggest remove both back tics and expr
j=`expr 2 * $1`
and use
j=$((2 * $1))

This ((i=i+1)) can be shorten to ((i++))
# 5  
Old 06-12-2013
Also, you need to reduce the length of out for each iteration of the second loop. Output command should be echo or printf not print:

Code:
i=1
  out=""
  ((j=2 * $1))
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
   done
  while [ $i -lt $j ]
  do
  out=${out%?}
  echo $out
  ((i=i+1))
  done

---------- Post updated at 10:29 PM ---------- Previous update was at 06:55 AM ----------

And here is a script to do a horizontal version:

Code:
  ##
 ####
######

Code:
i=1
while [ $i -le ${1:-3} ]
do
    out=${out}#
    printf "%${1:-3}s%s\n" $out $out
    ((i=i+1))
done

# 6  
Old 06-13-2013
Quote:
Originally Posted by Chubler_XL
Also, you need to reduce the length of out for each iteration of the second loop. Output command should be echo or printf not print:

Code:
i=1
  out=""
  ((j=2 * $1))
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
   done
  while [ $i -lt $j ]
  do
  out=${out%?}
  echo $out
  ((i=i+1))
  done

---------- Post updated at 10:29 PM ---------- Previous update was at 06:55 AM ----------

And here is a script to do a horizontal version:

Code:
  ##
 ####
######

Code:
i=1
while [ $i -le ${1:-3} ]
do
    out=${out}#
    printf "%${1:-3}s%s\n" $out $out
    ((i=i+1))
done


many thanks for the horizontal version script.However, I would like to ask that why my script is not working???

After making your changes below is the output

Code:
#
##
###
##
while1[14]: i++: 0403-053 Expression is not complete; more tokens expected.

Rgds,
TS

Last edited by Scrutinizer; 06-13-2013 at 11:19 AM.. Reason: code tags!
# 7  
Old 06-13-2013
Cant answer like that we havent the new script...
About your previous remark and my previous post, to work as you wanted the value in the second loop needed to be decremented which you did not so it would work only once then you would have always the same...

Here is the output and the script...
Code:
n12:/home/vbe/test $ sh 9007
9007[5]: test: 0403-004 Specify a parameter with this command.
9007[12]: 2*: 0403-053 Expression is not complete; more tokens expected.
n12:/home/vbe/test $ sh 9007 4
#
##
###
####
###
##
#

# the script 9007:
let i=1
let j=0
  out=""
  j=`expr 2*$1`
  while [ $i -le $1 ]
   do
    out="$out"#""
    echo $out
   ((i=i+1))
  done
  while [ $i -lt $j  ]
  do
     out=${out%?}  # missing in your first script...
     print $out
    #  print ${out%?}     # original print...
     ((i=i+1))
  done


Last edited by vbe; 06-13-2013 at 12:44 PM.. Reason: added original line of loop 2
This User Gave Thanks to vbe For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Distributing script projects, suggestions/ideas?

Heyas If you recall, not too long ago, i was asking about the GNU Autotools. The feedback on that was almost unisense, and me figured that it turned my (back then) +98% SHELL project into a +73% GROFF project... :( Felt a bit overhelmed, specialy since i didnt actualy use or need the true... (0 Replies)
Discussion started by: sea
0 Replies

2. Shell Programming and Scripting

WPAR monitoring shell script suggestions needed

Hi All, This is for WPAR monitoring shell script, earlier opened thread was closed, had to open a new thread, as suggested I have used script as below, But am trying to get the output in below format, need suggestions with it. Below is the lswpar output, required output format. ... (7 Replies)
Discussion started by: aix_admin_007
7 Replies

3. Shell Programming and Scripting

Unix Script -- Suggestions to list and kill PID's sequentially

Hi, I'm trying to write a script where i'm trying to grep the PID and the associated file and list them. Then execute the KILL command sequentially on the listed PID's for ".tra" files ==================================================== ps -aux | grep mine adm 27739 0.2 0.8 1131588... (12 Replies)
Discussion started by: murali1687
12 Replies

4. Shell Programming and Scripting

Need Suggestions to improve Perl script for checking malformed braces/brackets

Hi all, I've written a Perl script below that check and report for malformed braces. I have a UNIX ksh version and it took a couple of minutes to run on a 10000+ lines. With the Perl version it only took about 20 seconds so that is enough incentive for me to go Perl not to mention that I need... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. UNIX for Advanced & Expert Users

Need suggestions.......

Hello there....i am a final year comp science student.......i am thinking of doing my project on unix platform......which one do u suggest?thanx in advance... (3 Replies)
Discussion started by: theprasad1990
3 Replies

6. Shell Programming and Scripting

Script that accepts user input - Suggestions

Hi, I have a series of BASH shell scripts that process data. All of the scripts are controlled by a "master" script, where users specify their processing parameters. The sub-scripts, and the order they are called, depend on the values of these user-specified processing parameters. This method... (1 Reply)
Discussion started by: msb65
1 Replies

7. Shell Programming and Scripting

Suggestions/cleanup Bash script

Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just po in a new... (7 Replies)
Discussion started by: woodson2
7 Replies

8. Shell Programming and Scripting

Need suggestions about a datecheck script

I'm currently running a script that checks to see if a laptop is on the network, and if it is it backs up, if not it retries it later. Anyway, our backup scheduling has changed. I need to check if today's date is the Thursday after the first Wednesday of every month. This is made slightly more... (5 Replies)
Discussion started by: tsmurray
5 Replies

9. Shell Programming and Scripting

syntex error script any suggestions

a script with prompts user and returns the value of there home directory and full name #!/bin/bash echo "please enter your login ID" read login_id while $login_id -ne `grep $login_id /etc/passwd | cut -f1 -d:` is they anything wrong with it (5 Replies)
Discussion started by: kim187
5 Replies

10. Shell Programming and Scripting

Performance problem with my script ...suggestions pls

Hi , I have included my script below, pls read thro this req. I want my script to run for every hour , the problem is I CANNOT USE CRONTAB which is prohibited inside the company. My script does what it is supposed to do (to determine the memory and then send a email if it crosses a certain... (2 Replies)
Discussion started by: vivsiv
2 Replies
Login or Register to Ask a Question