Is it possible to do an "git status" and output the result into an echo? Or send the output as email?
I guess the email-thing is no problem but I stuck doing an echo.
What I got
clear
output="$(git status)"
echo output
But ... yeah, it won't work and I searched certain examples but they lead always ino a git status if,. and i need the output :( Is there way simple way to get this done
And, how to handle if this should be called on a remote system:
ssh ${SSHUSER}#${LIVE_HOST} << EOF
...
EOF
The echo is useless; all you need is
git status
If you genuinely need to store the output in a variable as well as print it, try
output=$(git status)
echo "$output"
To run it remotely, you don't need a here document at all;
ssh "${SSHUSER}#${LIVE_HOST}" git status
and again, if you need to store that in a variable, that would be
output=$(ssh "${SSHUSER}#${LIVE_HOST}" git status)
echo "$output"
If you really want to store the command in a here document, that's possible too:
ssh "${SSHUSER}#${LIVE_HOST}" <<\:
git status
:
or in Bash you could use a "here string":
ssh "${SSHUSER}#${LIVE_HOST}" <<<'git status'
If you want to send the result as email, a common arrangement is
git status | mail -s "Status report from xyz" you#example.com
but the mail command is poorly standardized, so you may have to consult a manual page or Google for a bit.
An echo "$output" would work better
echo ouput would just print output.
I just tried:
$ o=$(git status 2>&1); echo "$o"
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
It does not working because you missed the variables syntax.
Rewrite your code as follow:
clear
output="$(git status)"
echo "$output"
Related
I'd like to know if the user can input data.
In a script, it is usually possible to call read -r VARIABLE to request input from the user. However, this doesn't work in all environments: for example, in CI scripts, it's not possible for the user to input anything, and I'd like to substitute a default value in that case.
So far, I'm handling this with a timeout, like this:
echo "If you are a human, type 'ENTER' now. Otherwise, automatic installation will start in 10 seconds..."
read -t 10 -r _user_choice || _user_choice="no-user-here"
But honestly, that just looks ugly.
The solution doesn't have to use read, however it needs to be portable to all major distros that have Bash, so it's not possible to use packages that are not installed by default.
$ cat stdin.bash
if [[ -t 0 ]]; then
echo "stdin is a terminal, so the user can input data"
else
echo "stdin is connected to some other redirect or pipeline"
fi
and, demonstrating
$ bash stdin.bash
stdin is a terminal, so the user can input data
$ echo foo | bash stdin.bash
stdin is connected to some other redirect or pipeline
From help test output:
-t FD True if FD is opened on a terminal.
I'm making a script that reads passwords from pass to ssh, I want to stop the script if the hostname is not found but I don't know how to read the output of ssh
I've tried this
test=$(ssh user#nonvalidip)
check="ssh: Could not resolve hostname nonvalidip: Name or service not known"
if [ $test = $check ]; then
echo "Please enter a valid ip"
fi
but $test is empty, how can I read the output of ssh and make that the test variable?
Assuming that you are trying to run a shell script to gain access through a system through SSH then if that connection is successful to run a command. To do this there are multiple things you could do that are much simpler than trying to make an interpret less language work. What I would strongly suggest is that to solve the first issue is to make a smaller script within the script. Such as doing something like this:
ssh user#known_address << EOF - This will start the session and keep everything running beneath it until it reaches the term EOF
Using this may help you later if you are in the Linux industry as well. The script should look something like this:
ssh user#known_address << EOF
scp /etc/passwd USER#your_address/Path
EOF - keep note that this is an example of what you can do but it is not very wise to keep extra copies of the password file laying around on other systems.
Then instead of using exact copies of what the system outputs you can simply use exit codes. This can be WAY more helpful along the way. You can receive the error codes of the last command you ran with echo $?
I cannot guarantee that this script will work but here's an example of what you can do
session() {ssh user#add << EOF; command1; command2; command3; EOF}; session; if $? == 1; echo "test failed".
Sources
https://www.shellhacks.com/ssh-execute-remote-command-script-linux/
Meaning of exit status 1 returned by linux command
https://www.shellscript.sh/functions.html
I am writing shell script to install my application. I have more number of commands in my script such as copy, unzip, move, if and so on. I want to know the error if any of the commands fails. Also I don't want to send exit codes other than zero.
Order of script installation(root-file.sh):-
./script-to-install-mongodb
./script-to-install-jdk8
./script-to-install-myapplicaiton
Sample script file:-
cp sourceDir destinationDir
unzip filename
if [ true]
// success code
if
I want to know by using variable or any message if any of my scripts command failed in root-file.sh.
I don't want to write code to check every command status. Sometimes cp or mv command may fail due to invalid directory. At the end of script execution, I want to know all commands were executed successfully or error in it?
Is there a way to do it?
Note: I am using shell script not bash
/* the status of your last command stores in special variable $?, you can define variable for $? doing export var=$? */
unzip filename
export unzipStatus=$?
./script1.sh
export script1Status=$?
if [ !["$unzipStatus" || "$script1Status"]]
then
echo "Everything successful!"
else
echo "unsuccessful"
fi
Well as you are using shell script to achieve this there's not much external tooling. So the default $? should be of help. You may want to check for retrieval value in between the script. The code will look like this:
./script_1
retval=$?
if $retval==0; then
echo "script_1 successfully executed ..."
continue
else;
echo "script_1 failed with error exit code !"
break
fi
./script_2
Lemme know if this added any value to your scenario.
Exception handling in linux shell scripting can be done as follows
command || fallback_command
If you have multiple commands then you can do
(command_one && command_two) || fallback_command
Here fallback_command can be an echo or log details in a file etc.
I don't know if you have tried putting set -x on top of your script to see detailed execution.
Want to give my 2 cents here. Run your shell like this
sh root-file.sh 2> errors.txt
grep patterns from errors.txt
grep -e "root-file.sh: line" -e "script-to-install-mongodb.sh: line" -e "script-to-install-jdk8.sh: line" -e "script-to-install-myapplicaiton.sh: line" errors.txt
Output of above grep command will display commands which had errors in it along with line no. Let say output is:-
test.sh: line 8: file3: Permission denied
You can just go and check line no.(here it is 8) which had issue. refer this go to line no. in vi.
or this can also be automated: grep specific line from your shell script. grep line with had issue here it is 8.
head -8 test1.sh |tail -1
hope it helps.
Basically I want to type show and it checks if there is a show command or alias is defined and fire it and it is not defined fires git show.
For example rm should do rm but checkout should do git checkout.
Is it possible to program this in bashrc?
This is surprisingly easy:
master tmp$ trap 'git $BASH_COMMAND' ERR
master tmp$ touch foo
master tmp$ rm foo
master tmp$ add foo
bash: add: command not found
fatal: pathspec 'tmp/foo' did not match any files
master tmp$ branch
bash: branch: command not found
aix
allocators
...
This runs the usual touch and rm commands, but because there is no add command it runs git add foo and because there is no branch command it runs git branch
The trap command is run on any error, so not only when a command isn't found. You would probably want to do something smarter e.g. run a script which checks whether $? is 127 (the code bash sets when a command is not found) and then checks if running it with git instead would work (e.g. by checking for a command called git-xxx where xxx is the first word of $BASH_COMMAND). I leave that as an exercise for the reader.
There's no simple and proper way to achieve what you need. I think the best to do is to make an alias in ~/.bashrc for every git commands.
But on many distros, if you check man git, there's some Main porcelain commands that looks like aliases.
You can list all of them using
PAGER= man git | grep -oP 'git-\w+(?=\()'
When bash cannot find a command, it calls command_not_found_handle (if defined). You can define it to look something like this:
command_not_found_handle () {
git "$#" || { echo "$1 not a 'git' command either"; exit 127; }
}
Add to your ~/.bashrc:
alias show='git show'
alias checkout='git checkout'
...
Be careful not to make an alias for a command that already does something else. This may break others programs.
Here's one way of getting a list of all your git commands:
git help -a | egrep '^ [a-zA-Z0-9]' | xargs -n1 | sed 's/--.*//' | sort -u
Or you could use what's in contrib/completion/git-completion.sh: (This is probably better since you'll probably be looping anyway. Note: You'll need to consider for duplicates, but they don't really matter for alias)
__git_list_all_commands ()
{
local i IFS=" "$'\n'
for i in $(git help -a|egrep '^ [a-zA-Z0-9]')
do
case $i in
*--*) : helper pattern;;
*) echo $i;;
esac
done
}
I made a shell script that get a remote file with ftp protocol, if the file is well downloaded, it launch another script in php with curl.It's kind of working right now but i have a few questions to improve:
Do the script is waiting the end of the download to execute the rest of the script ?Or during the time of download the script do the following instructions ?
I receive the first mail of beginning instruction but never the last ones (the one that get the result of the curl, and the one at the end of the script) how come ?
I would like to find a good way to disallow the script to be run more than once (if the archive has been downloaded) even if it's launch every hours with crontab ?
what is the difference between quit/bye/by at the end of the ftp connection ?
This is the shell script:
echo start of the script | mail -s "beginning of the script" krifur#krifur.com
cd /my_rep
HOST='domaine.com'
PORT='21'
USER='admin'
PASSWD='pass'
jour=$(date "+%Y%m%d")
FILE="file_"$jour".txt";
ftp -i -n $HOST $PORT <<EOF
quote USER $USER
quote PASS $PASSWD
cd firstlevel
cd end
get $FILE
quit
EOF
if test -f $FILE
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur#krifur.com
else
echo no file : $FILE | mail -s "no file" krifur#krifur.com
fi
echo this is the end of the script download | mail -s "end of script download" krifur#krifur.com
Do the script is waiting the end of
the download to execute the rest of
the script ?Or during the time of
download the script do the following
instructions ?
If you mean "Will the FTP command block until finished?" , the answer is yes.
I receive the first mail of beginning
instruction but never the last ones
(the one that get the result of the
curl, and the one at the end of the
script) how come ?
Take a look at your code:
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur#krifur.com
else
echo no file : $FILE | mail -s "no file" krifur#krifur.com
fi
What are the contents of $CurlRes and $FILE respectively? Try ${CurlRes} and ${FILE}. I'd also suggest quoting strings when using echo.
There is also a good chance that spam filters don't like the message, have you checked on that?
I would like to find a good way to
disallow the script to be run more
than once (if the archive has been
downloaded) even if it's launch every
hours with crontab ?
That could be done in a number of ways. Perhaps, upon success echo the file name to the bottom of something like successfully_downloaded.txt , then use grep to see if the file name is in the list. Depending on use, that file might get rather large .. so I'd also implement some means of rotating it.
From man (3) ftp:
bye Terminate the FTP session with the remote server and exit ftp.
An end of file will also terminate the session and exit.
quit A synonym for bye.
by is also a synonym for bye, as far as I know.
This should be avoided at all costs:
USER='admin'
PASSWD='pass'
Use ssh's scp with keys (no password prompt required):
Linux Journal article howto