Bash script error. What is wrong with this script? - bash

I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'

Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)

Related

Bash treating variable expansion as command while passing to other script

a=HDH b=udud c=jsjsj bash secondscript
The command above works. I'd like to save the assignments in a variable, like so:
value="\
a=HDH \
b=udud \
c=ududj \
"
$value bash secondscript
But it gives an error:
test.sh: line 9: a=HDH: command not found
Why? What can I do instead?
bash's taking first item a=HDH as a command, what you need is :
value=(
"a=HDH"
"b=udud"
"c=ududj"
)
env "${value[#]}" bash secondscript

Syntax error: end of file unexpected

I define a function in Makefile
define write_file
for i in $( seq 1 10 )
do
echo "1234567" >> "tmp/test.txt"
done
endef
And
pre:
mkdir -p exe tmp
${call write_file}
But when I make pre,I got error:
mkdir -p exe tmp
for i in
/bin/sh: 1: Syntax error: end of file unexpected
Each line of a make-recipe is a single command that make runs in a single
new shell. So you need to make the body of the macro write_file into a single
shell command.
In addition, make expands anything of the unescaped form $(....),
treating .... as a defined or undefined make expression. So in your
case, make expands $( seq 1 10 ) to nothing. To stop make doing
that and let the shell expand $( seq 1 10 ), you need to escape $ for make,
which you do by writing $$ instead. The same goes for any $ in a make-recipe
that you intend to be expanded by the shell.
Putting these points together, you want:
define write_file
for i in $$( seq 1 10 ); \
do \
echo "1234567" >> "tmp/test.txt"; \
done
endef

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 ==.

unix - how to construct IFS for passing in context of file

Am I constructing this properly? I can't figure how to debug this.
Basically, I'm trying to pass in a text file (login.info):
foo
bar
To this shell script to be used as parameters like so:
#/usr/bin/ksh
#Base dir
BASEDIR=/home/scripts
export BASEDIR;
#Key
KEY=$BASEDIR/login.info
export KEY;
IFS="
"
arr=( $(<$KEY) )
echo "username=${arr[0]} password=${arr[1]}"
Getting this error:
./tst.sh[12]: Syntax error at line 12 : `(' is not expected.
It seems like your version of ksh doesn't understand (...) in array assignment. Maybe this will work better:
set -A arr $(cat $KEY)

Error defining variable in bash

I am writing simple housekeeping script. this script contains following line of codes.This is a sample code i have extracted from the actual code since it is a big file.
#!/bin/bash
ARCHIVE_PATH=/product/file
FunctionA(){
ARCHIVE_USER=user1 # archive storage user name (default)
ARCHIVE_GROUP=group1 # archive storage user group (default)
functionB
}
functionB() {
_name_Project="PROJECT1"
_path_Componet1=/product/company/Componet1/Logs/
_path_Component2=/product/company/Componet2/Logs/
##Component1##
archive "$(_name_Project)" "$(_path_Componet1)" "filename1" "file.log"
}
archive(){
_name= $1
_path=$2
_filename=$3
_ignore_filename=$4
_today=`date + '%Y-%m-%d'`
_archive=${ARCHIVE_PATH}/${_name}_$(hostname)_$(_today).tar
if [ -d $_path];then
echo "it is a directory"
fi
}
FunctionA
When i run the above script , i get the following error
#localhost.localdomain[] $ sh testScript.sh
testScript.sh: line 69: _name_Component1: command not found
testScript.sh: line 69: _path_Component2: command not found
date: extra operand `%Y-%m-%d'
Try `date --help' for more information.
testScript.sh: line 86: _today: command not found
it is a directory
Could someone explain me what am i doing wrong here.
I see the line: _today=date + '%Y-%m-%d'
One error I spotted was resolved by removing the space between the + and the ' like so:
_today=date +'%Y-%m-%d'
I don't see where the _name_Component1 and _name_Component2 variables are declared so can't help there :)
Your variable expansions are incorrect -- you're using $() which is for executing a subshell substitution. You want ${}, i.e.:
archive "${_name_Project}" "${_path_Componet1}" "filename1" "file.log"
As for the date error, no space after the +.
a few things... you are using $(variable) when it should be ${variable}
on the date command, make sure there is no space between the + and the format
and you have name= $1, you don't want that space there

Resources