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)
}
Related
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
I'm trying to write a simple function that checks whether a variables is declared and non empty or not.
my_var="dummy_val"
function validate_var(){
${1:?"Variable is not set"}
}
validate_var my_var
validate_var my_var2
I get the following error:
script: line n: my_var: command not found
Is there a simple way I could reference the variable from the function argument?
This seems to work. It uses the ! variable indirection.
function validate_var(){
: ${!1:?"$1 is not set"}
}
Pass the variable by prepending a $. Also I've added an echo (as an example), otherwise your validate_var function tries to execute it's argument:
my_var="dummy_val"
function validate_var(){
echo ${1:?"Variable is not set"}
}
validate_var $my_var
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
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 ==.
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