Need Bash Scripting Assistances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Bash Scripting Assistances
# 1  
Old 02-10-2017
Need Bash Scripting Assistances

Hi

I need help with the following code:

Code:
#!/bin/bash
 log_file="/etc/file1.log"
 LIST="a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z 0 1 2 3 4 5 6 7 8 9 ! £ $ % ^ & ( ) , . / ; ' # [ ] { } : @ ~ < > ?"
for a in $LIST
 do
        for b in $LIST
        do
                for c in $LIST
                do
                        for d in $LIST
                        do
                                for e in $LIST
                                do
                                        for f in $LIST
                                        do
                                                for g in $LIST
                                                do
                                                        for h in $LIST
                                                        do
                                                                for i in $LIST
                                                                do
                                                                        for j in $LIST
                                                                        do
                                                                                for k in $LIST
                                                                                do
                                                                                        for l in $LIST
                                                                                        do
                                                                                                for m in $LIST
                                                                                                do
                                                                                                echo -n " $a$b$c$d$e$f$g$h$i$j$k$l$m " >> $log_file
                                                                                                echo -n " $a$b$c$d$e$f$g$h$i$j$k$l$m "
                                                                                                done
                                                                                        done
                                                                                done
                                                                        done
                                                                done
                                                        done
                                                done
                                        done
                                done
                        done
                done
        done
 done

Is there a way to shorten the above code and still produce the same outcome?

Thank you
# 2  
Old 02-10-2017
Yes - replace the two echoes with echo -n ... | tee $log_file

I'm not sure how you want to compare the resp. outputs - one single long line with 86 ^ 13 (roughly 1,4 10^25) words; even less sure what you might need that for.

Last edited by RudiC; 02-10-2017 at 10:36 AM..
# 3  
Old 02-10-2017
Imagine it like numbers. For three digits, you could run one loop which adds to the first digit, then the second on overflow, etc, and only quits when the third overflows. You can use an arbitrary number of digits this way, especially since BASH has arrays.

Code:
#!/bin/bash

LIST=(0 1 2 3 4 5 6 7 8 9)
INDEX=(0 0 0)

while [ ${INDEX[0]} -lt ${#LIST[@]} ]
do
        echo "${INDEX[@]}"

        I=$(( ${#INDEX[@]} - 1 )) # Which digit are we adding to

        ((INDEX[$I]++))

        # Carry 2 10 into 3 0
        while [ $I -gt 0 ] && [ ${INDEX[$I]} -ge ${#LIST[@]} ]
        do
                ((INDEX[$I] -= 10))     # Subtract ten from current digit
                ((I--))                 # Go back one digit
                ((INDEX[$I] ++ ))       # Carry one
        done
done

This counts from 0 0 0 - 9 9 9 that way, but can count symbols just as easily. It will need some changes to actually print symbols instead of digits.
# 4  
Old 02-10-2017
A recursive function can be nested a given times.
Code:
#!/bin/bash
log_file="./file1.log"
LIST="a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z 0 1 2 3 4 5 6 7 8 9 ! £ $ % ^ & ( ) , . / ; ' # [ ] { } : @ ~ < > ?"
# disable globbing; needed because of the unquoted $LIST that contains special characters like "?"
set -f

# the number of digits
depth=26

do_digit(){
if [[ $((depth)) -eq 0 ]]
then
  printf "%s\n" "$1"
  printf "%s\n" "$1" >&3
  return
fi
((depth-=1))
local i
for i in $LIST
do
  do_digit "$1$i"
done
((depth+=1))
}

# open $log_file once with descriptor 3
# writes to &3 will go there
do_digit 3>"$log_file"


Last edited by MadeInGermany; 02-10-2017 at 06:39 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 02-11-2017
You would need 182 Yottabyte of storage for the result, which is 200 times the estimated storage size of the entire Internet in 2014.

To write it, you would need to wait 110 billion years @ 50MB/s, so that is 24 times the age of Earth.

I can imagine the answer will no longer be relevant by then.

Last edited by Scrutinizer; 02-11-2017 at 03:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-11-2017
Q.e.d.
the power of a shell script!
Smilie
# 7  
Old 02-11-2017
Thanks for all the replies

Is there anyway to run the below lines within a bash script:
Code:
sudo head -n NUMBER FILE > FILE
sudo shuf FILE -o FILE

Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Bash Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Try running 'phone4 xyz' and see what happens. Modify your program so that if no matching name is found, an... (1 Reply)
Discussion started by: OmgHaxor
1 Replies

2. Shell Programming and Scripting

bash scripting

same script: 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the following lines in... (0 Replies)
Discussion started by: lamoul
0 Replies

3. Shell Programming and Scripting

bash scripting help

Hi Guys i have a <script?> that spits out the location of each printer using snpget here is the code for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
Discussion started by: ab52
2 Replies

4. Shell Programming and Scripting

Bash scripting

Try to imagine a flag: nnnnx nnnxx nnxxx nxxxx now imagine how it will output: 4 times the "n"and 1 times "x" 3 times "n"and" 2 times" x " .. etc. .. rhombus is the same only instead of "n" is there gap "and " x "is a few times to form the correct shape Can you help... (3 Replies)
Discussion started by: krcek12
3 Replies

5. Shell Programming and Scripting

bash scripting

Hello everyone!!!! I am new to this forum ...I have a problem. And I thought that you are expert :) so you can help me with that... I have a text file with maaany lines. Every line begins with something like that: <http aksjfskcuhrf kushkfsnus> <http sxnfrksehfsd gsdg r> I don't know if every... (16 Replies)
Discussion started by: mary_elen
16 Replies

6. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

7. Shell Programming and Scripting

please help with Bash Scripting????

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name) ... (2 Replies)
Discussion started by: eminjan
2 Replies

8. Shell Programming and Scripting

bash scripting help!!

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name)... (3 Replies)
Discussion started by: eminjan
3 Replies

9. Shell Programming and Scripting

Bash Scripting

Hello there peeps: There is a little piece of bash shell scripting problem i have, which i was hoping you could help me with. #!/bin/bash stored_word() { case $(( $$ % 8 )) in 0 ) echo "energy";; 1 ) echo "touch";; 2 ) echo "climbing";; 3 ) echo... (3 Replies)
Discussion started by: keyvan
3 Replies
Login or Register to Ask a Question