Random command not found error in shell script [duplicate] - shell

Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.
#!/bin/bash
function system_info
{
echo "function system_info"
}
$(system_info)
I get a function: command not found issue.

Bash is trying to evaluate the string that is outputted by the system_info function. You'll want to try the following, which will just simply run the function:
system_info
or to store the outputted value to a variable:
value=$(system_info)

You need to invoke the function by saying:
system_info
$(...) is used for command substitution.

Invoke the function inside the script with just the function name and execute the script from the shell
#!/bin/bash
function system_info {
echo "function system_info"
}
system_info

#!/bin/bash
function system_info
{
echo "function system_info"
}
echo $(system_info)
Kind of redundant but it works without the command not found error.
Or This:
#!/bin/bash
function system_info
{
echo "function\n system_info"
}
printf "$(system_info)"
If you want to use newline character.
You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php

Related

How to give a text file into a shell function?

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
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"
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

How can I assign the output of a function *with parameters* to a variable using bash?

Similar to How can I assign the output of a function to a variable using bash?, but slightly different.
If I have a function like this:
function scan {
echo "output"
}
...I can easily assign this to a variable like this:
VAR=$(scan)
Now, what if my function takes one or more parameters, how can I pass them to the function using the "shell expansion" syntax? E.g. this:
function greet {
echo "Hello, $1"
}
# Does not work
VAR=$(greet("John Doe"))
The above produces an error like this with my bash (version 5.0.3(1)-release):
$ ./foo.sh
./foo.sh: command substitution: line 8: syntax error near unexpected token `"John Doe"'
./foo.sh: command substitution: line 8: `greet("John Doe"))'
in this code :
function greet {
echo "Hello, $1"
}
# Does not work
VAR=$(greet("John Doe"))
the error is the parenthesis passing parameters.
try to do this:
function greet {
echo "Hello, $1"
}
# works
VAR=$(greet "John Doe")
it should work.
Explanation: when you use the $ an the parenthesis you have to write inside the parenthesis command as in a shell so the parameters are passed without parenthesis.
I have obviously been writing to much Java code lately. Drop the parentheses when calling the function and everything works flawlessly:
function greet {
echo "Hello, $1"
}
VAR=$(greet "John Doe")

AIX - bash script

I'm trying to implement a small bash script in AIX, but I'm having some problems. Bellow you can find a example. I have another question, if I want to add the script to Crontab, I think I'll have problems to call serverStatus.sh from IBM, how can avoid this problem.
#!/usr/bin/sh
WAS_HOME="/usr/IBM/WebSphere/AppServer/profiles/bpmnprd01/"
function StatusCheck()
{
$WAS_HOME/bin/serverStatus.sh BPM.AppTarget.bpmnprd01.0 -username admin -password admin
status=$(cat /usr/IBM/WebSphere/AppServer/profiles/bpmnprd01/logs/BPM.AppTarget.xxxxx/serverStatus.log| awk '{ if (NF > 0) { last = $NF } } END { print last }' "$#")
text="STOPPED"
if [[ $text == $status ]]
then
echo "OK"
else
echo "NOK"
fi
}
function start()
{
StatusCheck
}
start
-----------------------
when I try to execute the script above, I get the following error:
[root#bpmnprd01]/root/health_check# ./servers_check.sh
./servers_check.sh[7]: 0403-057 Syntax error at line 7 : `(' is not expected.
...after this I search on google, and I found some examples without "()" on subroutine.But I got this:
[root#bpmnprd01]/root/health_check# ./servers_check.sh
./servers_check.sh[30]: 0403-057 Syntax error at line 33 : `StatusCheck' is not expected.
Thanks in Advance
Tiago
AIX has a true bourne shell living in /bin/sh, not sure about /usr/bin/sh, but would expect that to be Bourne shell as well.
Change your script heading line (the #shebang!) to
#!/usr/bin/bash
Or the result of which bash
IHTH
You are using bash specific syntax but calling the script with sh, which has more limited capabilities. Since you want to use sh, you can use a tool like checkbashisms or shellcheck to help uncover non-portable syntax.
The immediate problem is that function foo() { ..; } is not a POSIX compliant function definition, and you should drop the keyword function and use just foo() { ..; }.
Your shell may also be lacking [[ ]] in which case you should use [ ] instead, with = instead of ==.

Bash Function -> Command not found

Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.
#!/bin/bash
function system_info
{
echo "function system_info"
}
$(system_info)
I get a function: command not found issue.
Bash is trying to evaluate the string that is outputted by the system_info function. You'll want to try the following, which will just simply run the function:
system_info
or to store the outputted value to a variable:
value=$(system_info)
You need to invoke the function by saying:
system_info
$(...) is used for command substitution.
Invoke the function inside the script with just the function name and execute the script from the shell
#!/bin/bash
function system_info {
echo "function system_info"
}
system_info
#!/bin/bash
function system_info
{
echo "function system_info"
}
echo $(system_info)
Kind of redundant but it works without the command not found error.
Or This:
#!/bin/bash
function system_info
{
echo "function\n system_info"
}
printf "$(system_info)"
If you want to use newline character.
You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php

command working in command line but cannot generalize as function

In this question
#SiegeX gives a nice way of cleaning the bash PATH variable from
duplicate entries:
PATH=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<<"${PATH%:}")
this works well when I type it in the command line.
I tried using this in a bash function to be able to apply it to other variables:
function dupremove()
{
${1}=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<<"${1%:}")
}
but when I execute it it gives the error:
> dupremove PATH
bash: PATH=PATH:: command not found
Any idea on I can write the function?
This works for me (TM)
function dupremove
{
eval path=\$$1
export $1=$(awk 'BEGIN{ORS=":";RS="[:\n]"}!a[$0]++' <<< $path)
}

Resources