While loop issue on second column using IFS - bash

This seems simple, list the directory from the first field then list the directory from the second field. The fields from the input file are comma separated, e.g.: XXXX1111111111112222,cool.com.
I run the command:
./list_directories some_file.csv
The list_directories script is this:
#!/bin/bash
INPUT=$1
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read WORKING STORE
do
echo $STORE
ls $STORE
echo $WORKING
ls $WORKING
done < $INPUT
IFS=$OLDIFS
Here's the output:
/pathtothe/som/coolplace/Imlookingfor/cool.com/place/123/XXXX1111111111112222 : No such file or directorye/Imlookingfor/cool.com/place/123/XXXX1111111111112222 /pathtothe/som/coolplace/Imlookingfor/cool.com/placing/123/XXXX1111111111112222 fileindir.txt otherfileindir.txt lastofthefilesindir.txt
I know that both directories exist. Not sure if I'm getting caught up on the loop or on the IFS.

Related

Copy word from 1 file to another using Bash

I'm trying to copy over only specific words from 1 text file to another.
this is what is inside my username2.txt:
jason,visitor
mark,staff
orlando,visitor
I'm trying to copy over only jason and orlando to another txt file so it'll just be like:
jason
orlando
this is currently my bash script (as of right now, it only creates a blank txt file):
#!/bin/bash
username="username2.txt"
while IFS=, read username1 group1; do
if [ $group1 = "visitor" ]; then
awk $username1 $username > reportvisitors.txt
fi
done < $username
This could be easily done with awk and we do not need to use a while loop for this since awk itself could read Input_file itself.
username="username2.txt"
awk -F, '$2=="visitor"{print $1}' "$username" > "reportvisitors.txt"
Explanation: Simple explanation is, making field separator as , for all lines of Input_file(which is a shell variable named username). Then inside main program checking condition $2=="visitor" to check if 2nd field is visitor then printing 1st column. At last of this awk program sending output to reportvisitors.txt.
Replace
awk $username1 $username > reportvisitors.txt
with
echo "$username1" >> reportvisitors.txt
Update
#!/bin/bash
username="username2.txt"
while IFS=, read username1 group1; do
if [ "$group1" = "visitor" ]; then
echo "$username1"
fi
done < $username > reportvisitors.txt
You have to edit the line 7 to
echo $username1 >> reportvisitors.txt
The Code should look like
#!/bin/bash
This part makes sure that an already existing file with the name is deleted
FILE=reportvisitors.txt
if test -f "$FILE"; then
rm $FILE
fi
from here on I have only edited line 5
username="username2.txt"
while IFS=, read username1 group1; do
if [ $group1 = "visitor" ]; then
echo $username1 >> reportvisitors.txt
fi
done < $username

Use key/value data from a file in a shell script

I have a file called Year.txt
Year2000= 1/2/3/4/
Year2001= 5/6/7/8/
Year2002= 9/10/11/12/
....
....
....
Year2020= 100/101/102/
etc and so on
I need to take this Year.txt as reference in my another script some sample.sh
sample.sh
source /home/user/Year.txt
d=cp $filename $1
echo $d
sample.sh Year2000(passing Year2000 as first argument)
**I need to cut the second part after = if I pass Year2000 as my argument and paste this 1/2/3/4/ in my statement
**I need to cut the second part after = if I pass Year2001 as my argument and paste this 5/6/7/8/ in my copy statement
etc..
I need output like this:
Input1 sample.sh Year2000
Output: cp somefile.txt 1/2/3/4/
Input2: sample.sh Year2001
Output: cp somefile.txt 5/6/7/8/
In Short -- I need to take the reference from another file and generate the copy statement
Don't source files that aren't legal bash code. In this case, an associative array lets you store as many key/value pairs as you need inside a single variable.
#!/usr/bin/env bash
case $BASH_VERSION in ''|[123].*) echo "ERROR: Needs bash 4.0 or newer" >&2; exit 1;; esac
year_name=$1
file_name=$2
[[ $file_name ]] || { echo "Usage: $0 year-name file-name" >&2; exit 1; }
# Read year.txt, and generate a map
declare -A dirs_by_year=( )
while IFS='= ' read -r k v; do
dirs_by_year[$k]=$v
done <Year.txt
if ! [[ ${dirs_by_year[$year_name]} ]]; then
echo "ERROR: User specified year $1, but input file does not have a directory for it" >&2
echo " ...defined years follow:" >&2
declare -p dirs_by_year >&2 # print array definition to show what we read
exit 1
fi
# generate and write a cp command
printf '%q ' cp "$file_name" "${dirs_by_year[$year_name]}"

Compare $1 with another string in bash

I've spent 2 hours with an if statement, that never works like I want:
#should return true
if [ "$1" == "355258054414904" ]; then
Here is the whole script:
#!/bin/bash
param=$1
INPUT=simu_900_imei_user_pass.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT ime not found"; exit 99; }
while read imei email pass
do
echo "First Parameter-IMEI: $1"
if [ "$1" == "355258054414904" ]; then
echo "GOOD"
fi
done < $INPUT
IFS=$OLDIFS
This is the output of the script:
First Parameter-IMEI: 355258054414904
First Parameter-IMEI: 355258054414904
First Parameter-IMEI: 355258054414904
I have seen a lot of pages about the subject, but I can't make it work :(
EDIT: I Join the content of csv for better understanding ! Tx for your help !
4790057be1803096,user1,pass1
355258054414904,juju,capp
4790057be1803096,user2,pass2
358854053154579,user3,pass3
The reason $1 does not match is because $1 means the first parameter given to the script on the command line, while you want it to match the first field read from the file. That value is in $imei.
You probably meant:
if [ "$imei" == "355258054414904" ]; then
echo "GOOD"
fi
Since it is inside the loop where you read input file line by line.
To check content of $1 use:
cat -vet <<< "$1"
UPDATE: To strip \r from $1 have this at top:
param=$(tr -d '\r' <<< "$1")
And then use "$param" in rest of your script.
To test string equality with [ you want to use a single '=' sign.

reading text file - bash

I am trying to read a text file-"info.txt" which contains the following information
info.txt
1,john,23
2,mary,21
what I want to do is to store each columns into a variable and print any one of the columns out.
I know this may seems simple to you guys but I am new to writing bash script, I only know how to read the file but I don't know how to delimit the , away and need help. Thanks.
while read -r columnOne columnTwo columnThree
do
echo $columnOne
done < "info.txt"
output
1,
2,
expected output
1
2
You need to set the record separator:
while IFS=, read -r columnOne columnTwo columnThree
do
echo "$columnOne"
done < info.txt
Is good to check if the file exists too.
#!/bin/bash
INPUT=./info.txt
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read -r columnOne columnTwo columnThree
do
echo "columnOne : $columnOne"
echo "columnTwo : $columnTwo"
echo "columnThree : $columnThree"
done < $INPUT
IFS=$OLDIFS

Bash: Formatting results inside for loop from a ls command

How come the additional 'Line' insideecho "Line $line" is not prepended to all files inside the for loop?
#!/bin/bash
INPUT=targets.csv
IFS=","
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read target user password path
do
result=$(sshpass -p "$password" ssh -n "$user"#"$target" ls "$path"*file* 2>/dev/null)
if [ $? -ne 0 ]
then
echo "No Heap dumps detected."
else
echo "Found a Heap dump! Possible OOM issue detected"
for line in $result
do
echo "Line $line"
done
fi
done < $INPUT
.csv file contents ..
rob#laptop:~/scripts$ cat targets.csv
server.com,root,passw0rd,/root/
script output ..
rob#laptop:~/scripts$ ./checkForHeapdump.sh
Found a Heap dump! Possible OOM issue detected
Line file1.txt
file2.txt
The statement:
for line in $result
performs word splitting on $result to get each element that $line should be set to. Word splitting uses the delimiters in $IFS. Earlier in the script you set this to just ,. So this loop will iterate over comma-separated data in $result. Since there aren't any commas in it, it's just a single element.
If you want to split it by lines, do:
IFS="
"
for line in $result

Resources