Need Bash Scripting Assistances

Hi

I need help with the following 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

Yes - replace the two echo es 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.

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.

#!/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.

A recursive function can be nested a given times.

#!/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"
1 Like

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.

1 Like

Q.e.d.
the power of a shell script!
:wink:

Thanks for all the replies

Is there anyway to run the below lines within a bash script:

sudo head -n NUMBER FILE > FILE
sudo shuf FILE -o FILE

Thank you

Hello,

Yes, there is: open up a file in a text editor, type those two commands in, save that file, mark it as executable, and run it in Bash. Basically, if you can type it, you can script it (more or less, anyway). That's all a Bash script is, at its heart - a sequence of commands that the shell runs through in order that (with a few exceptions) are no different from anything you could have done by hand.

I think at this point there's a question that needs to be asked so that you can give people a better idea of what to suggest to help you: what are you actually trying to achieve with these commands, and why do you want to put these two commands in a script ? If you can explain the problem you're trying to solve, people may be able to offer more practical suggestions.

1 Like