Jump to content

run command in terminal


Recommended Posts

Hi, 

 

I've created a custom bash command of "passw" which runs a python script to create an xkcd style password. I can run this from any terminal screen and it outputs a password.

 

function passw() {
python3 ~/icloud/python_projects/PassGen/xkcd_password_generator.py
}

 

I would like to create a workflow to output to clipboard and notification, but I am struggling with the run script action, I can't seem to find a basic tutorial on using it. 

 

so far I have tried 

query=passw
echo -n $query

 

and 

 

passw

 

the log file says that passw command is not found. I'd be grateful for any help!

 

Link to comment
2 hours ago, hybridjosto said:

I can't seem to find a basic tutorial on using it

 

It's just regular UNIX scripting. Read tutorials on that.

 

2 hours ago, hybridjosto said:

the log file says that passw command is not found. I'd be grateful for any help!

 

You haven't created a bash command, you've created a function. You haven't said which file you defined it in, but the reason it isn't working is because whichever file that is isn't loaded when you execute bash code in Alfred.

 

Most shell configuration files are only loaded when you start an interactive session (i.e. in a terminal). They aren't loaded when you run a shell from another program, such as Alfred, so you can't use any functions or aliases you've defined in your dotfiles.

 

You also shouldn't source the dotfiles in an Alfred script because it's very slow (if it even works).

 

The solution is to call your Python script directly instead of via a convenience function you’ve written to make it easier to do from your terminal command line. And unless you’re using Catalina, where python3 is in a standard directory (/usr/bin/python3), you’ll have to call it via its full path (because, as noted, Alfred doesn’t use your shell environment).

 

2 hours ago, hybridjosto said:

query=passw

echo -n $query

 

This is wrong. You’re just assigning the string “passw” to the variable $query. If you want to assign the output of a command/function to a variable in a shell, do query="$( passw )"

 

Edited by deanishe
Link to comment
10 hours ago, hybridjosto said:

query="$( python3 ~/icloud/python_projects/PassGen/xkcd_password_generator.py  )"

 

 

I you’re saving the output of the command as a variable just so you can echo -n and not get a newline, it’s redundant.

 

Do:

echo -n "$(python3 ~/icloud/python_projects/PassGen/xkcd_password_generator.py)"

And you’ll do the same in a single line without needing to save the output. Alternatively:

python3 ~/icloud/python_projects/PassGen/xkcd_password_generator.py | tr -d '\n'

Which will run your command and then strip the newline. Another option is to output the result from the python script directly without the newline, or use Alfred’s Transform Utility to strip whitespace.

Link to comment
2 hours ago, vitor said:

Another option is to output the result from the python script directly without the newline

 

Yeah. This would probably be the most sensible solution, seeing as you're probably never going to want a newline added to the password. Use sys.stdout.write() instead of print, or pass the end='' argument to print.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...