Calling shell script within a script - linux

Below are my two script codes:
**a.sh**
#!/bin/sh
SRC_PATH="/xx/xxxx"
HOST='ftp.xxx.xxx.com'
USER='xxxx'
PASS='xxx'
FTP_SRC_PATH='out'
**b.sh**
#!/bin/sh
/xx/xxx/a.sh
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
binary
prompt off
cd $FTP_SRC_PATH
lcd $SRC_PATH
mget IMS_*.ZIP
bye
END_SCRIPT
My issue is when I am running b.sh , it is not calling a.sh and using the variables defined in it to connect to ftp server.
I have seen many solutions already online but things doesn't seem to work.
Please help

You can include a.shwith the dot command:
. /xx/xxx/a.sh

You can call the script a from within b as follows:
sh ./a.sh
Make sure both the shell scripts are in same file path. Also, you can pass the value within scripts as follows:
sh ./a.sh $d $e
The values passed here can be accessed as $1 $2.

Here is an example:
a.sh:
!/bin/bash
yourvalue="test"
sh ./b.sh $yourvalue
and I'm able to access the variable passed here in the shell script b as $1.

Related

Failed to execute script by calling another script

I have two shell scripts for two different user user 'a' and user 'root':
a.sh for user 'a'
export CODE=`pwd | cut -d / -f 5`
isEnabled=`grep -i isEnabled $HOME/$CODE/config/code.properties | awk -F'=' '{print $2}'`
b.sh for user 'root'
su - a -c "sh /export/home/a/a1/bin/a.sh status
As you can see, root execute script b.sh which execute script a.sh
but actually it failed since it does not find the path to the script since the
variable $CODE in a.sh probably missing in run time while running b.sh
after investigation i tired the following in b.sh:
. /path/to/a.sh
source /path/to/a.sh
. /path/to/a.sh "$CODE"
. $(/path/to/a.sh $CODE)
Can someone please advise?
Thank you all.
The variable $CODE should be there, but it will be set to a's home directory. Try using basename $0 instead of pwd. That will get the directory the script is being executed from, rather than the current working directory of user executing it.

Running two commands in same variable in Bash CGI

In my Bash CGI script, I take a command passed as GET parameter and execute it. This could be:
CMD='ls -al'
$CMD
Which works fine and produces expected output. But if I try to pass two commands with
CMD='ls -al; echo hello'
$CMD
or
CMD='ls -al && echo hello'
$CMD
neither command gets executed.
How can I run multiple commands from the same line/variable in my bash CGI?
You can execute variables as bash code using bash:
# UNSAFE, DO NOT USE
cmd='ls -al; echo hello'
bash -c "$cmd"
Alternatively, depending on the context you want to run it in, you can use eval "$cmd" to run it as if it was a line in your own script, rather than a separate piece of shell code to execute:
# UNSAFE, DO NOT USE
cmd='ls -al; echo hello'
eval "$cmd"
Both of these methods have serious implications for security and correctness, so I felt I had to add warnings to prevent them from being copied out of context.
For your remote shell or root kit specifically meant to run insecure user input, you can ignore the warnings.

Difference between sh and sh -c in unix

Consider I have script like given below.
demo.sh:
name=$1
age=$2
echo $name
echo $age
when I execute the script like
sh demo.sh ARUN 24
Output is:
ARUN
24
But, When I execute the script like
sh -c demo.sh ARUN 24
Output is nothing.
I know if i use sh -c arguments will be assigned starting from $0. But only file name is getting assigned to $0.
How can i assign more than one parameters when is use sh -c?
Please explain me how it works.
Use this syntax instead:
sh -c "demo.sh ARUN 24"
In your case those two arguments where passed to the sh command, not the demo.sh script. Put them in quotes to pass them trought sh to the script.
Note the easiest way would be to make the script executable and add a hashbang (#!) line to it. The scripts would then look as follows:
#!/bin/sh
name=$1
age=$2
echo $name
echo $age
Make it executable with:
chmod +x demo.sh
Then run it as follows (without the interpreter sh, the interpreter is now gathered from the #! line):
./demo.sh ARUN 24

read more than one parameters right after the command in bash

I am making a bash script and I want it to be just one line, meaning it will not have any interaction with the users and the parameters will be on the same line as the command. Once the user clicks return, it will output the result.
Right now, I have something that looks like this:
#! \bin\bash
read $1 $2
do something with $1 and $2
However, if I name my script "test" when I type in test at the beginning of the command line, I will have to type enter for the rest of the script to be executed. How should I modify it so that I can run the entire thing on just one line?
The standard way to pass parameters to a script is not with read (which actively waits for input from stdin), but just to call your script with the parameters on the same line:
./my_script.sh param1 param2
Then inside the script, you can access these parameters using $1, $2, etc. Example (note also the first line - this describes what shell should be used to run the script, and should be a valid path - ie /bin/bash, not backslashes):
#!/bin/bash
echo "First: $1 Second: $2"
Then call the script:
$ ./my_script.sh Hello There
First: Hello Second: There
What you probably need is this :
You script name test.sh contains the following:
#!/bin/bash
echo "$1 $2"
Then, change permission so that you can execute the script on the command line :
chmod u+x test.sh
and run the script with arguments (two in this case) :
./test.sh tik tak
will return
tik tak

piping a script to ssh, unable to set variable

I expected it to print v=1. Why does this print v=?
cat<<DONE|ssh user#host
v=1
echo v=$v
DONE
On host, bash is the shell.
Your code is equivalent to:
echo "v=1;echo v=$v"|ssh user#host
What you want is:
echo 'v=1;echo v=$v'|ssh user#host
You can achieve this by using cat<<'DONE' instead of cat<<DONE.
Variables are expanded inside heredocs. You need to escape the $ with a \

Resources