0

I am writing a bash script that accepts a manifest.txt file. It should cycle through each row of that text file and call another script to pass the text string from the row of the text file after separating the string of text by the space delimiter.

I am stuck on figuring out how to split the text string; I tried to use cut -d' ', but it has not worked for me.

Here's the example of the text file:

20200451310 B.315
30203131340 Pam 3781, no.1
20200461200 B.16
20200471180 B.116, B.198
20200471190 B.129
10107291410 B.102
30203141220 Pam 3870, no. 1
20200481160 B.525

Here's the bash script I have so far:

#!/bin/bash 
IFS=$'\n'       # make newlines the only separator
set -f          # disable globbing
for i in $(cat < "${manifest.txt}"); do
  echo " $i"
  uid = ${i | cut -d' ' -f1}
  string = ${i | cut -d' ' -f2-10}   
  echo "uid is ${uid}"
  echo "string is ${string}"
  /anotherscript.sh ${uid} ${string}
done

The result should be, for example:

/anotherscript.sh "30203141220" "Pam 3870, no. 1"
4
  • Have a look at BashFAQ/001 and Looping through the content of a file in Bash to see how to read a file line by line. Commented Dec 4, 2019 at 21:33
  • Then, use shellcheck.net to get pointers about common mistakes (spaces around = in assignments etc.). Commented Dec 4, 2019 at 21:33
  • You're using ${ }, but you mean $( ) for command substitution. The command i | cut -d' ' -f1 won't work, you have to pipe $i to cut somehow, for example with cut -d' ' -f1 <<< "$i". Commented Dec 4, 2019 at 21:34
  • But really, instead of using cut, you should read each line into two variables and let Bash to the splitting for you. Commented Dec 4, 2019 at 21:35

1 Answer 1

1

You can simplify your script to this:

#!/usr/bin/env bash

while read -r uid string; do
    ./anotherscript.sh "$uid" "$string"
done < manifest.txt

A laundry list of mistakes in your attempt:

  • "${manifest.txt}" tries to perform variable expansion on manifest.txt, which isn't a variable name, leading to an error; you want manifest.txt instead
  • cat < somefile can be just cat somefile
  • In Bash, for i in $(cat somefile) can be simplified to for i in $(< somefile)
  • But really, you want to read lines with

    read IFS= read -r i; do ...; done < manifest.txt
    
  • i | cut -d' ' -f1 doesn't make cut read $i; you have to use something like cut -d' ' -f1 <<< "$i" instead

  • ${somecommand} is the wrong syntax; for command substitution, you want $(somecommand) instead
  • Assignments can't have blanks around =: uid = something should be uid=something
  • To split a string, read into multiple variables with read instead of using cut:

    while read -r uid string; do ... done < manifest.txt
    
  • /anotherscript.sh is probably the wrong path and should be ./anotherscript.sh instead

  • arguments in /anotherscript.sh ${uid} ${string} should be quoted:

    ./anotherscript.sh "$uid" "$string"
    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.