Home > database >  How to give a text file into a shell function?
How to give a text file into a shell function?

Time:06-08

Hi I'm trying to make a function which should get a text file and then do some things on it and then echo. But when I try to execute it, it says syntax error near unexpected token `"$cat"'

#!/bin/usr/bash
  
cat=$(< cat_dialogue.txt)
 
function test_cat (){
    echo $1
}

test_cat($cat)

desired output:

>meow meow

CodePudding user response:

Here is an example BASH function that strips a branchname:

#create function
function strip () {
#create local variable that takes input and fills $TEXT
    local TEXT=$1

    #stips the branch number from the branchname
    echo $TEXT | sed 's/-[0-9]*//2'
}

strip "testbranch-12345-28796"

hope it helps :) also check the BASH documentation as mentioned by @joshmeranda

CodePudding user response:

Your program may look like the following. Note all differences. Check your scripts with shellcheck.

#!/usr/bin/env bash
  
cat=$(< cat_dialogue.txt)
 
test_cat() {
    echo "$1"
}

test_cat "$cat"
  • Related