Passing value from one shell script to other - linux

This is what I am trying
script1
var=10
sh -x script2 "$var"
script2
su - someuser << EOF
1
cd dir
echo "This is : $1 Error" >> somefile
2
2
0
exit
EOF
Everything in script2 is executing. When I am checking the file "somefile" the output is
This is : Error
It is not showing the value of var

It is working fine for me:
cat s1
var=10
sh -x /tmp/s2 "$var"
cat s2
su - my_id << EOF
id
echo $1
EOF
./s1
+ su - my_id
+ 0<<
id
echo 10
my_id's Password: <<< su is prompting for my password
uid=222(my_id) gid=222(my_group) ...
10
Because you are not adding the #!/bin/xxxxx I believe the default is to either execute /bin/sh or maybe what $SHELL is set to. Check that both of those are what you expect. Maybe add the explicit #!/bin/ksh (or #!/bin/sh ...) to make sure you are getting the shell that you want / expect.

Related

while running this programe i occured one error as ambigous redirect

This is my following bash script
cat >> $file_name
And I receive this kind of error:
./l7.sh: line 12: $file_name: ambiguous redirect
Here are the full code
https://github.com/vats147/public/blob/main/l7.sh
And Why I am getting this error? even my syntax is correct.
Into the parameter file_name you must assign $1, which will pass to the current file as an input parameter.
#! /bin/bash
echo -e " Enter file name : \c"
read file_name=$1
if [ -f $file_name ]
then
if [ -w $file_name ]
then
echo " type some text data. to quit press enter "
#cat > $file_name(single angular bracket use for overwritten)
#cat >> $file_name(two angular bracket use for appending a text)
cat >> $file_name
else
echo " file not have write permission"
fi
else
echo "file not exist"
fi
These are positional arguments of the script.
Executing ./script.sh Hello World will make
$0 = ./script.sh
$1 = Hello
$2 = World
Note
If you execute ./script.sh, $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh.

Incorrect exit status being fetched after executing a script

I have 2 scripts . I'm invoking one script from the other for capturing the exit status.
Import.sh
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
temp.sh
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
schema_remap_AANV02_UAT2.txt
AANVPXE;Arju4578;PANVPXE
AANVSL;Arj0098;PANVSL
AANV;Arju1345;PANV
the .txt file does not have read permission(make sure that you do not give read permission), so the script should fail by returning the exit status as exit $? .
Below is the output after i run temp.sh
+ cat schema_remap_AANV02_UAT2.txt
+ egrep -w 'PANV|PANVPXE'
cat: schema_remap_AANV02_UAT2.txt: Permission denied
+ exit 1
Return code =
Internal scripts is exiting with exit 1(since cat command is failing) , but inside temp.sh i'm not getting the expected value while capturing the return code.
I want to make sure that whichever command fails in import.sh , the script should return with appropriate exit status.
To get the exit code of your script Import.sh instead of its output, change the script temp.sh to
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
RC=$?
echo "Return code = $RC"
or simply
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
echo "Return code = $?"
See the comments for hints how to fix/improve your scripts.
I tried to understand the way you invoke your child script ( Import ) into the parent script ( temp.sh ). Well let me show you what is happening
Import Script
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
This script will exit with something different than 0 when a problem with the grep occurs, so if the pattern you are looking for it is not there, it will fail.
$ echo "hello" | egrep -i "bye"
$ echo $?
1
Then you are running this script from other program
Launcher
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
Here is where you have the problem. You are calling the script like it was a function and expecting a result. The variable RC is getting whatever output your script is sending to the STDOUT , nothing else. RC will be always empty, because your script does not send anything to the STDOUT. So, you must understand the difference between getting the result from the child and evaluating what return code produced the child program.
Let me show you an example of what I just explained to you using my own scripts. I have two scripts: the child.sh is just a sqlplus to Oracle. the parent invokes the child the same way you do.
$ more child.sh
#/bin/bash
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" << eof
whenever sqlerror exit failure;
select * from dual ;
eof
if [[ $? -eq 0 ]];
then
exit 0;
else
exit 99;
fi
$ more parent.sh
#!/bin/bash
# run child
var=`/orabatch/ftpcpl/log/child.sh`
echo $var
$ ./child.sh
D
-
X
$ echo $?
0
$ ./parent.sh
D - X
$ echo $?
0
As you see, my parent is getting whatever the child script is sending to the STDOUT. Now let's force an error in the child script to verify that my parent script is still exiting as ok:
$ ./child.sh
select * from dual0
*
ERROR at line 1:
ORA-00942: table or view does not exist
$ ./parent.sh
ERROR at line 1:
ORA-00942: table or view does not exist
$ echo $?
0
As you can see, the output of my operation is the error in the first, however not as an error, but as an output. my parent has ended ok, even you can see that there was an error.
I would rewrite the script as follows:
#/bin/bash
my_path=/app/arjun/scripts
$my_path/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
result=$?
if [ ${result} -ne 0 ];
then
echo "error"
exit 2;
fi

Unable to get input from user in shell script which is called by git hook pre-push script

I am running a shell script from another shell script which is a git-hook pre-push.
This is the content of .git/hooks/pre-push:
#!/usr/bin/env bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]; then
sh test.sh
if [ $? != 0 ]; then
echo "Error"
exit 1
fi
else
exit 0
fi
This is the content of test.sh:
#!/bin/bash
run_base=1
run_test () {
read -p "enter varname: " varname
echo $varname
}
time {
if [ "$run_base" = "0" ] ; then
echo "skipped"
else
time { run_test ; }
echo "run_test done";
fi
}
If I run pre-push script directly, then it works fine, but it doesn't work when I execute git push origin master and pre-push gets triggered automatically.
read in test.sh is not being executed when I trigger the pre-push hook script. Do I need to do anything specific in order to execute read in test.sh which is called from pre-push?
I just test it on my computer and it works perfectly,
isoto#hal9014-2 ~/w/test> ./pre-push
enter varname: asd
asd
real 0m1.361s
user 0m0.000s
sys 0m0.000s
run_test done
real 0m1.361s
user 0m0.000s
sys 0m0.000s
So, the only thing that I did was to add executable permissions, chmod +x *
And also I put both scripts in the same directory, besides everything should work.
Found the answer, I had to add < /dev/tty at the end of read:
read -p "enter varname: " varname < /dev/tty
echo $varname

Get return value of command run in background with pipe and tee command

I want to get the return value of command run in background with pipe so i have below sample code.
#!/bin/bash
export RETVALUE="0"
CMD='ls ThisFileDoesNotExit'
LOG='tee -a log.txt'
$CMD | $LOG ; RETVALUE=${PIPESTATUS[0]} &
#$CMD | $LOG ; echo ${PIPESTATUS[0]} & // This print ret value 2
wait
echo "Return Value of ls is $RETVALUE"
Output:
Return Value of ls is 0 // It should print value 2
If i echo the return value from same command then it print correct return value.
But if store it in RETVALUE variable then it shows wrong value.
The problem is due to the & sign. This puts the RETVALUE assignment into the background, thus this command executes in a different environment than the current script, so the variables in your script do no get updated.
You also don't need to export the RETVALUE.
Also the wait command is not necessary, as bash does not process the next command until it has finished the previous one (unless you use the & to put it in the background)
#!/bin/bash
RETVALUE="0"
CMD='ls ThisFileDoesNotExit'
LOG='tee -a log.txt'
$CMD | $LOG
RETVALUE=${PIPESTATUS[0]}
echo "Return Value of ls is $RETVALUE"
EDIT: If you need to launch the process in the background, you will be forced to create a new script in order to recover the PIPESTATUS value, due to this variable is volatile. A possible solution is:
#!/bin/bash
CMD='ls ThisFileDoesNotExit'
LOG='tee -a log.txt'
TMPSCRIPT="file1.sh"
echo '#!/bin/bash' > $TMPSCRIPT
echo "$CMD |$LOG" >> $TMPSCRIPT
echo 'exit ${PIPESTATUS[0]}' >> $TMPSCRIPT
chmod +x $TMPSCRIPT
./$TMPSCRIPT &
MYPID=$!
wait $MYPID
RETVALUE=$?
rm $TMPSCRIPT
echo "Return Value of ls is $RETVALUE"

same shell script not working on different environmnet

I have just basic knowledge on Unix and know how to run shell script.
Process: we have created a batch (ex:test.bat)which will call shell script(shelltest.sh) by passing arguments.
when user enter a command on UI application front,the test.bat file will invoke with command-line( ex: test.bat "-h checkwriter -A 20141203")
bat file is calling sh file and passing arguments ,but the problems is i have a function/method in the sh file to verify certain checks ,this check is within the while block.
here the function is calling but exiting without entering into the while condition.
The same shell script working fine on testing environments but where as which is failing on PROD environment.
test.bat
sh shelltest.sh
shelltest.sh
#!/bin/sh
#set -x
PROGRAMNAME=`basename $0`
echo "Prog name is $PROGRAM"
QUIT=0
Check_test()
{
echo "Check1:$#"
echo "Check2:$#"
echo "Check3:$*"
echo "Param1 : is $1"
echo "Param2 : is $2"
echo "Param3 : is $3"
while [ $# -ne 0 ]
do
case `echo $1 | tr "[:lower:]" "[:upper:]"` in
-K)
echo "Inside MYVAR before:${1}"
shift
MYVAR=${1}
echo "Inside MYVAR after:${1}"
;;
*)
echo "Inside what is"
;;
esac
if [ $# -ne 0 ]
then
shift
fi
done
if [ "${MYVAR}" == "" ]
then
echo "Failed to enter into while condition "
QUIT=1;
fi
}
Check_test $*
Output of the above code for respective environment is,
**'---The out put in Testing environment (-K checkwriter -A 20141203)
Prog name is shelltest.sh
Check1 : -K checkwriter -A 20141203
Check2 : 4
Check3 : -K checkwriter -A 20141203
'after entered in while loop
Inside MYVAR before:-K
Inside MYVAR after: checkwriter**
**'----The output in PROD environment
Prog name is (no file is coming...it is empty)
Check1 : -K checkwriter -A 20141203
Check2 : 4
Check3 : -K checkwriter -A 20141203
'Not entering into while loop
Failed to enter into while condition**
the above shell script giving out put what am expecting,but in PROD environment it is failing to enter into the while loop. and one more 'basename' also coming as empty in prod.
Please,any one can suggest me how i can fix this issue.
Note: we are running on windows machine but to support unix commands we got installed some 3rd party apis.

Resources