-1

I'm relatively new to bash scripting and I would like someone to explain this properly, thank you. Here is my code:

#! /bin/bash

echo "first arg: $1"
echo "first arg: $2"
var="$( grep -rnw $1 -e $2 | cut -d ":" -f1 )"
var2=$( grep -rnw $1 -e $2 | cut -d ":" -f1 | awk '{print substr($0,length,1)}')

echo "$var"
echo "$var2"

The problem I have is with the output, the script I'm trying to write is a c++ function searcher, so upon launching my script I have 2 arguments, one for the directory and the second one as the function name. This is how my output looks like:

first arg: Projekt
first arg: iseven
Projekt/AX/include/ax.h
Projekt/AX/src/ax.cpp
h
p

Now my question is: how do can I save the line by line output as a variable, so that later on I can use var as a path, or to use var2 as a character to compare. My plan was to use IF() statements to determine the type, idea: IF(last_char == p){echo:"something"}What I've tried was this question: Capturing multiple line output into a Bash variable and then giving it an array. So my code looked like: "${var[0]}". Please explain how can I use my line output later on, as variables.

ekaeo
  • 157
  • 10
  • 2
    https://stackoverflow.com/a/17602863/9072753 It's `echo "${var[0]}"`. And first you have to `var=($(....))`. Research bash arrays on the net first, there are many good resources.. – KamilCuk Dec 28 '20 at 08:06
  • I'm sorry, but yes, "${var[0]}" is exactly what I wrote and it doesn't work, is there any way to make each output line as a separate string? – ekaeo Dec 28 '20 at 09:16
  • In your question you say `echo "$var[0]"` is what you wrote. That's not the same as `echo "${var[0]}"`. If you tell us in what way "it doesn't work" then we can probably help you debug the issue. – Ed Morton Dec 28 '20 at 23:52
  • I've edited the question now, sorry it was my fault, since I don't work with bash often, I just used "c++ syntax" without thinking – ekaeo Dec 29 '20 at 14:58

1 Answers1

0

I'd use readarray to populate an array variable just in case there's spaces in your command's output that shouldn't be used as field separators that would end up messing up foo=( ... ). And you can use shell parameter expansion substring syntax to get the last character of a variable; no need for that awk bit in your var2:

#!/usr/bin/env bash

readarray -t lines < <(printf "%s\n" "Projekt/AX/include/ax.h" "Projekt/AX/src/ax.cpp")

for line in "${lines[@]}"; do
    printf "%s\n%s\n" "$line" "${line: -1}" # Note the space before the -1
done

will display

Projekt/AX/include/ax.h
h
Projekt/AX/src/ax.cpp
p
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • ```my_array=() while IFS= read -r line; do my_array+=( "$line" ) done < <( grep -rnw $1 -e $2 | cut -d ":" -f1)``` – ekaeo Dec 29 '20 at 15:00
  • What I did was use this command, since I need to acmes those variables later, correct me if I'm wrong, but I don't think I can't do that with your solution, because I'll be comparing this later on in the script – ekaeo Dec 29 '20 at 15:01
  • @ekaeo Er... use `readarray`, not a loop. Much more efficient, simpler, clearer and less error prone. That's why I suggested it in the first place. And then use the array and its elements however. – Shawn Dec 29 '20 at 16:30
  • I've tried it out, changed it a lil bit, but ye, thank you, tho it does say that readarray isn't as error prone as you say. I'm guessing less errors compared to what I did – ekaeo Dec 29 '20 at 16:43