LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2020, 02:40 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Rep: Reputation: 2
URL as a Command


When I enter a git repo url in terminal it says:
Code:
% https://github.com/chmln/sd.git        
zsh: no such file or directory: https://github.com/chmln/sd.git
I want https://github.com/chmln/sd.git to act as a command which will

Code:
git clone https://github.com/chmln/sd.git
cd sd
How can I do that?
 
Old 07-26-2020, 04:31 AM   #2
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
I tried preexec hook.

Code:
preexec () { 
    print ">>>preexec start<<<"
    # print -l ${(qqq)@}

    if  [[ ${(qqq)@} =~ ^https.* ]]
    then
    echo URL
    fi
    print ">>>preexec end<<<"

}
The output is

Code:
% https://github.com/chmln/sd.git
>>>preexec start<<<
>>>preexec end<<<
zsh: no such file or directory: https://github.com/chmln/sd.git
 
Old 07-26-2020, 04:44 AM   #3
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
The code that is working is

Code:
preexec () { 
    print ">>>preexec start<<<"

    if  [[ $@[1] =~ ^https.*git ]]
    then
        git clone $@[1] && cd "$(basename $@[1] .git)"
    else 
    echo did not match
    fi
    print ">>>preexec end<<<"

}
However, there is a little annoyance.

Code:
% https://github.com/chmln/sd.git
>>>preexec start<<<
Cloning into 'sd'...
remote: Enumerating objects: 78, done.
remote: Counting objects: 100% (78/78), done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 792 (delta 34), reused 48 (delta 15), pack-reused 714
Receiving objects: 100% (792/792), 239.98 KiB | 318.00 KiB/s, done.
Resolving deltas: 100% (456/456), done.
>>>preexec end<<<
zsh: no such file or directory: https://github.com/chmln/sd.git
You can see that the message zsh: no such file or directory: https://github.com/chmln/sd.git is still there.
 
Old 07-26-2020, 05:53 AM   #4
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
I got a solution where it uses

Code:
function command_not_found_handle {
        if [[ "$1" =~ "^(https|http|git|(git\\+)?ssh)://.*\$" ]]; then
                echo "Do something: $1"
                return 1
        else
                return 0
        fi
}
But in my zsh the error message (no such file or directory...) does not seem to be handled by this handler.

I am guessing this because, I used

Code:
if [[ -x /usr/lib/command-not-found ]] ; then
	if (( ! ${+functions[command_not_found_handler]} )) ; then
		function command_not_found_handler {
			echo "The following command is not valid: \""$1\"""
			[[ -x /usr/lib/command-not-found ]] || return 1
			/usr/lib/command-not-found -- ${1+"$1"} && :
		}
	fi
fi
But The following command is not valid: is not shown before the line zsh: no such file or directory:
 
Old 07-26-2020, 08:45 AM   #5
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
Code:
function _accept-line-with-url {

    if  [[ $BUFFER =~ ^https.*git ]]
    then
        # zle -U "git clone $BUFFER && cd \"$(basename $BUFFER .git)\""

        printf "\n"
        git clone $BUFFER && cd "$(basename $BUFFER .git)"

        zle kill-whole-line
        zle .accept-line
    else 
        zle .accept-line
    fi
    
}

zle -N accept-line _accept-line-with-url
 
Old 07-26-2020, 12:17 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by blueray View Post
When I enter a git repo url in terminal it says:
Code:
% https://github.com/chmln/sd.git        
zsh: no such file or directory: https://github.com/chmln/sd.git
I want https://github.com/chmln/sd.git to act as a command which will

Code:
git clone https://github.com/chmln/sd.git
cd sd
How can I do that?
If you want to execute git with a URL as the argument just do that.
The shell is always going to report "no such file" for a URL unless you have an actual file named sd.gin in a directory named chmln in a directory named github.com in a directory named https:

Something like this in a file name rungit:
Code:
#!/bin/zsh
git $1
cd sd
then
Code:
rungit https://github.com/chmln/sd.git
 
Old 07-26-2020, 12:45 PM   #7
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
Use the code in my last post and put it in your .zshrc

Alhamdulillah, it is actually working now.
 
  


Reply

Tags
bash, zsh



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Redirect URL to another URL when the link is down with Squid marx_long Linux - Newbie 1 06-13-2010 08:59 PM
on url "http://localhost/" error ""The requested URL could not be retrieved " renuaseri Linux - Newbie 1 04-07-2009 12:23 AM
Update part of a URL to another URL in MySQL guest Linux - Software 2 02-15-2009 09:02 PM
ERROR The requested URL could not be retrieved While trying to retrieve the URL: /re Niceman2005 Linux - General 1 06-29-2005 09:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration