LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2015, 08:34 AM   #16
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308

would be nice to post the actual script...
 
Old 11-10-2015, 08:52 AM   #17
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
would be nice to post the actual script...
We tried something else.
Didn't work.

Quote:
#!/bin/bash

echo "Write a number between 1-9"
read number

if(($number == {tall1..tall9})); then


function tall1 {
for i in {1..1}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall2 {
for i in {1..2}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall3 {
for i in {1..3}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall4 {
for i in {1..4}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall5 {
for i in {1..5}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall6 {
for i in {1..6}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall7 {
for i in {1..7}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall8 {
for i in {1..8}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

function tall9 {
for i in {1..9}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}

echo
done
before that we still got this:

Quote:
#!/bin/bash
for i in {1..9}; do
for j in $(eval echo {1..$i}); do
echo -n $i
done
echo
done
 
Old 11-10-2015, 08:59 AM   #18
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Can we do it like this?

We don't know if the syntax is right.

Can we make functions for every number 1-9? Or is that completely wrong? Are we thinking right?

Quote:
#!/bin/bash


echo "Input a number between 1-9"
read input

if [$input == 1]; then
number


function number1 {
for i in {1..1}; do
for j in $(eval echo {1..$i}); do
echo -ne $i
return
}
 
Old 11-10-2015, 09:08 AM   #19
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
have you checked your script? it is syntactically incorrect (the last one)
Code:
if [ $input -eq 1 ]; then
would be much better:
1. need space before and after [ and ]
2. use -eq to compare numbers (not ==)



here: http://www.cyberciti.biz/faq/bash-for-loop/ you can find a better tip to organize your loops
 
Old 11-10-2015, 09:54 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am wondering if you read my post at all?? You still seem to be just throwing commands around with very little understanding. You will need to use google and look at the links provided by myself and others. We fully understand that you are a beginner, but you are not taking any of the advice provided.

So i will start at the beginning again. As pointed out by myself and others, the first part of your task is to assess if the user has passed you any numbers when starting the script.
So go away and use whatever search tool you like (maybe google) and find out what $# means and is used for. Before looking at any part of your script if i do not see this I will ignore all future posts.
 
Old 11-10-2015, 11:18 AM   #21
hortageno
Member
 
Registered: Aug 2015
Distribution: Ubuntu 22.04 LTS
Posts: 240

Rep: Reputation: 67
You use this

Code:
 for j in $(eval echo {1..$i}); do
for the inner loop. Why don't you do the same for the outer loop, only with the variable you got from the read command instead if $i. Saves you all these functions for each case.
 
Old 11-10-2015, 04:28 PM   #22
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I agree with grail; one thing at a time.

Later(!) you may want to look into this for generating a list of nums
Code:
for i in $(seq  1 $1)
do
    echo $i
done
 
Old 11-12-2015, 05:33 AM   #23
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I am wondering if you read my post at all?? You still seem to be just throwing commands around with very little understanding. You will need to use google and look at the links provided by myself and others. We fully understand that you are a beginner, but you are not taking any of the advice provided.

So i will start at the beginning again. As pointed out by myself and others, the first part of your task is to assess if the user has passed you any numbers when starting the script.
So go away and use whatever search tool you like (maybe google) and find out what $# means and is used for. Before looking at any part of your script if i do not see this I will ignore all future posts.
Yeah, sorry man. Didn't really understand anything before we started on scratch again. We found a solution with using functions.

Here is our answer:

Quote:
#!/bin/bash

echo "Choose a number between 1-9"
close() {
for((i=1;i<=n;i++))
do
for((k=i;k<=n;k++))
do
echo -ne " ";
done
for((j=1;j<=i;j++))
do
echo -e "$i \c"
done
echo ""
done
}
read n
if [[ n -le 9 && n -ge 1 ]]; then
close
fi
Our next task was to make a game. We manage to make it perfectly without any help, so that was very motivating.

No though, we are struggeling again.

The new task is:

You will here develop a program that reports how many files, symbolic links and directories
contained in a directory. The program will be developed in two stages as explained below, but you do
only deliver the final version.

3a) First create a program that expects one parameter: the name of a directory. If it does not get
any parameter, it should use the working directory. The program will use for loop to go through
catalog and count how many ordinary files it finds, where many symbolic links, and how
Many directories.

This information shall program write to standard output, with explanation. If the directory
contains 3 files, 2 symbolic links, and 5 subdirectories, the program should write "3 files, 2
symbolic links, directories 5, a total of 10 "to standard output.

We've come so far:

Quote:
#!/bin/bash

shopt -s globstar
for filer in **/*
do
if [ -d "$filer" ];then
((d++))
elif [[ -L "$f" ]]
then (("symlink++"))
else (("filer++"))
fi
done
echo "antall filer: $f"
echo "antall kataloger: $d"
 
Old 11-12-2015, 06:33 AM   #24
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
please use code instead of quote if possible.
You use filer in your for cycle and also as a counter for files. That will definitely make troubles, will not work.
Also sometimes you use the variable f which never get initialized.
 
Old 11-12-2015, 06:51 AM   #25
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
please use code instead of quote if possible.
You use filer in your for cycle and also as a counter for files. That will definitely make troubles, will not work.
Also sometimes you use the variable f which never get initialized.
Yes, I will use code instead of quotes.
Can you answer this though?

How can we find a solution to make the user skip prinf by pressing enter.
Thanks. Heres the code.

Code:
#!/bin/bash
echo "Which catalog do you want?"
 
printf "Velg katalog:\n"
select s in **/; do test -n "$s" && break; echo ">>> No choice"; done
cd "$s" && pwd

shopt -s globstar
for filer in */*
do
  if [ -d "$filer" ];then
    ((d++))
  elif [ -f "$filer" ];then
     ((f++))
  fi
	if [ -L "$filer" ];then
		((L++))
  fi

done

echo "antall filer: $f"
echo "antall kataloger: $d"
echo "antall softlinks: $L"
echo "Totalt:" $((f+d+L))
 
Old 11-12-2015, 06:54 AM   #26
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
How can we find a solution to make the user skip prinf by pressing enter.
I'm really sorry, but I do not understand what do you want to achieve
 
Old 11-12-2015, 07:03 AM   #27
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I'm really sorry, but I do not understand what do you want to achieve
I would like my program to be able to list all files and directories in a path if the user wishes to input one, and if the user just presses "enter" it will list all files and directories in the current directory.

Sorry for not being able to convey what I would l like the program to do, I´m still trying to wrap my head around the whole thing.


Thanks in advance.
 
Old 11-12-2015, 08:19 AM   #28
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I see. You can try:
select s in . **/
probably that helps...
 
Old 11-12-2015, 08:32 AM   #29
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I see. You can try:
select s in . **/
probably that helps...
Didn't work.

We want the oppertunity for the user to skip the first part, so it will show us files and folders in our workdirectory.

Looks like this now:
Quote:
#!/bin/bash
echo "Which catalog do you want?"

printf "Velg katalog:\n"
select s in **/; do test -n "$s" && break; echo ">>> No choice"; done
cd "$s" && pwd

shopt -s globstar
for filer in */*
do
if [ -d "$filer" ];then
((d++))
elif [ -f "$filer" ];then
((f++))
fi
if [ -L "$filer" ];then
((L++))
fi

done

echo "antall filer: $f"
echo "antall kataloger: $d"
echo "antall softlinks: $L"
echo "Totalt:" $((f+d+L))

Last edited by kjeska; 11-12-2015 at 08:43 AM.
 
Old 11-12-2015, 10:06 AM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I am not sure if you are handing these scripts in to be marked, but if you are, you will lose marks on the first task as it does not meet the brief:
Quote:
The program can be started with numbers parameter between 1 and 9.
At no time do you accept or test for any parameters at the command line, so if I were to run:
Code:
#!/bin/bash

echo "Choose a number between 1-9"
close() {
  for((i=1;i<=n;i++)) 
  do  
    for((k=i;k<=n;k++)) 
    do  
      echo -ne " ";
    done
    for((j=1;j<=i;j++))
    do  
      echo -e "$i \c"
    done  
    echo ""
  done
}
read n
if [[ n -le 9 && n -ge 1 ]]; then
  close
fi
And ran it with an argument:
Code:
$ ./pyramid.sh 5
Choose a number between 1-9
So instead of printing my pyramid up to 5 I get asked again what number I would like to choose.

As a learning exercise, here is a list of things I would mark you down on if grading the work:

1. Script has no help option in case the user does not know what choices there are when running it, ie. like the fact you can pass an argument at the command line

2. Command line arguments are ignored (so does not meet the brief)

3. Question to user and reading of the answer are so far apart that following the code is confusing

4. Users response is not tested at all to see if it is a number, ie. I can enter the letter 'a' and the code simply finishes

5. As with point 4, if the user enters a number outside the range the script simply finishes and you have no message to advise why (even though in a simple script like this it is obvious, it is a good practice to get into)

6. When testing for numbers, like in all your for loops, it is better to use (()) as you can then use standard arithmetic signs like <= or >=

7. 'close' as a function name doesn't seem to make much sense?? Maybe draw_pyramid or write_nums, but something more on what the function does so later when you look at it (maybe 5 years later) you don't have to read all the code to discover that the function does not 'close' anything

8. You go to the trouble of creating a function but then use global variables. Look up the reserved word local

9. No message to advise the script has completed successfully (not always required but might have given you extra marks )

10. No comments at all throughout code to advise what is happening (again thinking of looking at the code in the future)

On the positive side:

1. Code adequately tests number range

2. Correct data is shown once number provided

As a tip, maybe look into the printf command as it could replace the need for your first internal if (ie. where you print the spaces before printing the first number on the line)

You may wish to apply some of the above to your next task as well

Last edited by grail; 11-12-2015 at 10:07 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to make a diamond with progressing & rectracting numbers andrew.comly Programming 13 12-02-2014 10:55 AM
making a number pyramid imran042 Programming 20 06-16-2012 11:02 AM
gcc and linux pyramid board shabi Linux - Embedded & Single-board computer 3 06-23-2010 11:42 PM
How do i make a script that checks that my input is letters or numbers ? ministeren Programming 13 05-20-2010 09:36 AM
how do I make lpr always print page numbers fakie_flip Linux - Software 2 11-30-2007 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration