Load List From Text File To Bash Script - bash

I've a .txt file which contains
abc.com
google.com
....
....
yahoo.com
And I'm interested in loading it to a bash script as a list (i.e. Domain_List=( "abc.com" "google.com" .... "yahoo.com") ). Is it possible to do?
Additional information, once the list is obtained it is used in a for loop and if statements.
for i in "${Domain_list[#]}
do
if grep -q "${Domain_list[counter]}" domains.log
....
....
fi
....
let counter=counter+1
done
Thank you,
Update:
I've changed the format to Domain_list=( "google.com .... "yahoo.com" ), and using source Doamin.txt allows me to use Domain_list as a list in the bash script.
#!/bin/bash
counter=0
source domain.txt
for i in "${domain_list[#]}"
do
echo "${domain_list[counter]}"
let counter=counter+1
done
echo "$counter"

Suppose, your datafile name is web.txt. Using command substitution (backtics) and cat, the array can be built. Pl. see the following code,
myarray=(`cat web.txt`)
noofelements=${#myarray[*]}
#now traverse the array
counter=0
while [ $counter -lt $noofelements ]
do
echo " Element $counter is ${myarray[$counter]}"
counter=$(( $counter + 1 ))
done

Domain_list=()
while read addr
do
Domain_list+=($addr)
done < addresses.txt
That should store each line of the text file into the array.

I used the source command, and it works fine.
#!/bin/bash
counter=0
source domain.txt
for i in "${domain_list[#]}"
do
echo "${domain_list[counter]}"
let counter=counter+1
done
echo "$counter"

There's no need for a counter if we're sourcing the list from a file. You can simply iterate through the list and echo the value.
#!/bin/bash
source domain.txt
for i in ${domain_list[#]}
do
echo $i
done

Related

How I prepend a string on each item of an array?

I have the following bash script:
#!/bin/bash
items=('mysql_apache','postgresql_apache','maria_apache')
string=""
for i in "${array[#]}"; do
string=$string" -t"$i
done
echo $string
But if I output the string I won't get the expected result:
-t 'mysql_apache' -t 'postgresql_apache' -t 'maria_apache'
DO you have any Idea how I can do this?
Edit 1
I tried the following:
#!/bin/bash
items=('mysql_apache' 'postgresql_apache' 'maria_apache')
string=""
for i in "${array[#]}"; do
string=$string" -t"$i
done
echo $string
But I still do not get the expected output.
Array elements are separated by whitespace, not commas. Also, items != array.
#! /bin/bash
items=(mysql_apache postgresql_apache maria_apache)
string=""
for i in "${items[#]}"; do
string+=" -t $i"
done
echo $string
But you don't need a loop at all:
items=(mysql_apache postgresql_apache maria_apache)
echo ${items[#]/#/-t }
The substitution can be applied to every element of an array. The /# matches at the start of each string.
You're close. Your forgot to change ${array[#]} in the for loop to what your array was named: items or specifically ${items[#]} You also needed a few other little changes, see below:
#!/bin/bash
declare -a items=('mysql_apache' 'postgresql_apache' 'maria_apache')
string=""
for i in "${items[#]}"; do
string=${string}" -t "$i
done
echo $string
Lastly if you want to see what is happening you can add temporary echo statements to see what if anything is changing:
for i in "${items[#]}"; do
string=${string}" -t "$i
echo >>>$string<<<
done

Bash: Echo file contents precedes with counter

I am looking for a bash script which reads the file content and it should echo the output as mentioned below:
Input File: file.txt
host1a
host2b
host3c
host4e
I want my output like:
--START--
opt1:host1a
opt2:host2b
opt3:host3c
opt4:host4e
--END--
there is a many possibilities, try this way for example.
#!/bin/bash
opt="1";
while read line;
do
if [ ! -z "$line" ]
then
echo "opt$opt:$line"
opt=$(($opt+1))
fi
done <your_input_file.txt

Creating A Loop to name files in BASH

I need to create a script that creates the number of files a user specifies, using the name and file ext the user specifies. My loop fails, and only one file is created.
#!/bin/bash
#arguements variables
file=$1
ext=$2
numFiles=$3
count=0
#for loop for 5 repetitions
while [ $3 -ge count ]; do
touch ${1}.${2}.${3}
done
echo " "$3" Files where created using the name "$1" and extension "$2" "
To create count files with name file and extension ext and suffixed with a number from 1 to count, try:
for i in $(seq "$count"); do touch "$file.$ext.$i"; done
It is not necessary to specify extension, you could use shell parameter extension. You could also use for loop with index:
#! /bin/bash
FILE_NAME=$1
NUM_OF_FILES=$2 || 0
for ((IDX=0; IDX < NUM_OF_FILES; IDX+=1)) ; do
# for fname.tar.gz result is fname0.tar.gz, fname1.tar.gz, ...
touch "${FILE_NAME%%.*}$IDX.${FILE_NAME#*.}"
done
You forgot to increase count. Try this:
#arguements variables
file=$1
ext=$2
numFiles=$3
count=0
#for loop for 5 repetitions
while [ ${numFiles} -ge ${count} ]; do
touch ${1}.${2}.${count}
let "count+=1"
done
echo " "$3" Files where created using the name "$1" and extension "$2" "

How to use a text file for multiple variable in bash

I want to make an bash script for things I use much and for easy access of things but I want to make an firstrun setup that saves the typed paths to programs or commands in a txt file. But how can I do that. And how can I include the lines of the text file to multiple variables?
After a lot of testing I could use the 2 anwsers given. I need to store a variable directly to a textfile and not asking a user for his details and then stores that to a file
So I want it to be like this
if [[ -d "/home/$(whoami)/.minecraft" && ! -L "/home/$(whoami)/.minecraft" ]] ; then
echo "Minecraft found"
minecraft="/home/$(whoami)/Desktop/shortcuts/Minecraft.jar" > safetofile
# This ^ needs to be stored on a line in the textfile
else
echo "No Minecraft found"
fi
if [[ -d "/home/$(whoami)/.technic" && ! -L "/home/$(whoami)/.technic" ]]; then
echo "Technic found"
technic="/home/$(whoami)/Desktop/shortcuts/TechnicLauncher.jar" > safetofile
# This ^ also needs to be stored on an other line in the textfile
else
echo "No Technic found"
fi
I really want to have an anwser to this because I want to script bash. I already experience in bash scripting.
Here's an example:
#!/bin/bash
if [[ -f ~/.myname ]]
then
name=$(< ~/.myname)
else
echo "First time setup. Please enter your name:"
read name
echo "$name" > ~/.myname
fi
echo "Hello $name!"
The first time this script is run, it will ask the user for their name and save it. The next time, it will load the name from the file instead of asking.
#!/bin/bash
# file to save the vars
init_file=~/.init_vars.txt
# save_to_file - subroutine to read var and save to file
# first arg is the var, assumes init_file already exists
save_to_file()
{
echo "Enter $1:"
read val
# check if val has any spaces in them, you will need to quote them if so
case "$val" in
*\ *)
# quote with double quotes before saving to init_file
echo "$1=\"$val\"" >> $init_file
;;
*)
# save var=val to file
echo "$1=$val" >> $init_file
;;
esac
}
if [[ ! -f $init_file ]]
then
# init_file doesnt exist, this will come here only once
# create an empty init_file
touch $init_file
# vars to be read and saved in file, modify accordingly
for var in "name" "age" "country"
do
# call subroutine
save_to_file "$var"
done
fi
# init_file now has three entries,
# name=val1
# age=val2
# country=val3
# source the init_file which will read and execute commands from init_file,
# which set the three variables
. ${init_file}
# echo to make sure it is working
echo $name $age $country

Shell script loop over filenames from 0000 to 1500

I'm trying to create a shell script that shall convert certain Files according to some rules. The files are organized like this:
file_0000.adat
file_0001.adat
file_0002.adat
file_0003.adat
...
file_0010.adat
file_0011.adat
...
file_9999.adat
My current script looks like this:
#!/bin/sh
lauf=$(expr 0)
for filename in ../distData/file_*.adat
do
...
lauf=$(expr $lauf + 1)
done;
But now I only want the files with numbers from 0000 to 1500. For the conversion I need the number of the file (done by the variable lauf in the script). Can't figure out how to do this right now.
Thanks for replys.
Try something like:
for i in $(seq -w 1500) ; do
if [ -f ../distData/file_${i}.adat ] ; then
# do whatever, the file number is ${i}
fi
done
I tried something like this to generate the number pattern of the file.
You can put the other part of the code to concatenate with file name and search for the file name
START=0
MAX=1500
while [ $START -le $MAX ]
do
PAT=$(printf "%04d" $START)
echo $START $PAT
START=`expr $START + 1`
done
FILES=$(ls ../distData/file_{0000..1500}.adat 2>/dev/null)
for FILE in ${FILES[#]}; do
#$FILE exists, do what you want
done
the redirect of stderr to /dev/null is to suppress output messages for files in the range that do not exist

Resources