This question already has answers here:
Difference between sh and Bash
(11 answers)
How to run a bash script via Cron
(3 answers)
Closed 7 months ago.
I made a bash script for Ubuntu server with C-style if-then clause if (( $var = 6 )). It works perfectly from command line, but stuck when I made cronjob for this script. I changed it to if [ $var -gt 6 ] and then cron began to work. I assume that cron uses different shell, but what type and what other differences with bash?
Related
This question already has answers here:
What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?
(4 answers)
Closed 2 years ago.
I've been doing lots of Linux based stuff with my time and I know that the ; is used to separate commands, and && runs command after the previous one is done.
But if anyone more knowledgeable then me can explain the difference between the two, that would be nice.
Here's a simple example:
whoami ; hostname
whoami && hostname
; will execute the second command whether or not the first returns without error.
&& is the bash AND logical operator, and will execute the second command only if the first returns succesfully without error.
The success of a command is determined by its exit status.
This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to assemble an rsync command in a bash variable and then execute it.
It looks something like this:
CMD="$RSYNC -a $REMOTE $LOCAL $LINK_DEST"
It gets executed like this
RSYNC_RESULT=$($CMD)
This works fine until I try to add add --rsync-path="sudo /usr/local/bin/rsync" to the mix (so that rsync runs as root on the remote).
RSYNC_PATH='--rsync-path="sudo /usr/local/bin/rsync"'
CMD="$RSYNC -a $RSYNC_PATH $REMOTE $LOCAL $LINK_DEST"
Now I get an error
Unexpected remote arg: user#remote.local:/Users/user/files/
rsync error: syntax or usage error (code 1) at main.c(1343) [sender=3.1.2]
I'm fairly certain it's connected to the quoting in the $RSYNC_PATH var and/or the $($CMD) bit, because I can paste the resulting command into a shell and it runs successfully.
Any ideas what I can do to make this work?
This question already has answers here:
get values from 'time' command via bash script [duplicate]
(4 answers)
How to store a substring of the output of "time" function in bash script
(3 answers)
Closed 5 years ago.
I'm looking to capture the execution time of an rsync transfer and then store that time in a variable for a later step in my bash script.
I have tried the following:
ELAPSED=$(time $(rsync -azh source/ dest &> /dev/null)) to no avail. The elapsed time is printed to the screen, but it is not saved in the ELAPSED variable.
I have also tried: ELAPSED=$(/usr/bin/time sh -c "rsync -azh source/ dest &> /dev/null") but this actually takes the sh output and stores that output into the variable, not the time.
Any insights on a better method or corrections to my attempts are appreciated in advance!
This question already has answers here:
How do I use a file grep comparison inside a bash if/else statement?
(5 answers)
How to kill a background process created in a script
(2 answers)
Closed 5 years ago.
I'm trying to stop a long running process as soon as the file /status contains the string Success.
I tried this without success:
cat & while [ `grep -q Success /status` ]; do sleep 1; done; kill %1
cat is the long running process that needs to be stopped when /status contains Success.
Cheers
This question already has answers here:
How to detect if a script is being sourced
(22 answers)
Closed 8 years ago.
Is there a way to check within a shell script (ksh) whether or not the script was started in the current shell?
Example
Start script in current shell with . (dot/source) command
$ . ./myscript
$ I run in the current environment!
Start script in own process
$ ./myscript
$ I run in my own process!
This is a simple trick you can use.
#!/bin/ksh
if [ ${.sh.file} != ${0} ]; then
echo I run in the current environment
else
echo I run in my own process
fi
Every Shell Has its Own PID ..
so you can use echo "$$" in ur script ..it will helps us find from where the Script is RAN .
i.e Difference in pid means they are run from different shells .