How to automatically execute a command inside a certain file

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
xskyjackerx

How to automatically execute a command inside a certain file

Post by xskyjackerx »

I am writing a script called First_Script,and i put it in my Scripting folder.I will continue to put my Scripts in the Scripting folder.I want to be able to just us this command line "First_Script"(or whatever the script name is.) and run the script.However right now i use ./First_Script.I would like to just type the designated directory in the ".profile" file were the script will be.So i dont have to type ./ every time(its annoying not hard).

Kinda like this... Image

However my .profile file looks like...
Image
So were do i put the command "export PATH=$PATH:directory"(Scripting)in the .profile file.

(I only want it to look in this directory for scripts)
Thanks
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Garvan
Level 4
Level 4
Posts: 300
Joined: Sun May 29, 2011 3:26 am
Location: Thailand
Contact:

Re: How to automatically execute a command inside a certain

Post by Garvan »

Put it on the last line. I think it needs to be in quotes, and you need to show where the script folder is. For example I tried this and it worked.

export PATH="$PATH:$HOME/script"

I do not know if export is required. You can try with and without.

Garvan
Notebook: DELL Latitude E5520, i5-2520M @ 2.50 GHz, 4GB RAM, Linux Mint 17.2 (2011)
Notebook: DELL Latitude 5280, i5-73000 @ 2.7 GHz., 16 GB RAM, Linux Mint 19.2 (2019)
mph426

Re: How to automatically execute a command inside a certain

Post by mph426 »

If you do, or want to do a lot of command line stuff, you may want to consider a few more variables.

Here's my .bash_profile.

Code: Select all

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

# User specific environment and startup programs

LS_OPTIONS='--color'
PATH=$PATH:$HOME/bin
TERM=xterm
LESS=MMr

set -o vi 

export PATH LS_OPTIONS TERM LESS EDITOR

alias ll='ls -lh $LS_OPTIONS'
alias llm='ls -alh  $LS_OPTIONS| less'
alias lrt='ls -alhrt $LS_OPTIONS'
alias ls='ls  $LS_OPTIONS'
alias lsd='ls -lhd */ 2>/dev/null  $LS_OPTIONS'
alias lsnd='ls -lh  |grep -v ^d  $LS_OPTIONS'
alias cp="cp"
alias mv="mv"
alias df="df -h"
 
As you can see the variables are set individually, then exported on a single line. Exporting variables makes them global. That is to say that other instances of the shell can see them.
Oh yea, make sure to read the man pages for the terminal you want use. Usually a -ls will make it a login session and read the .bash_profile in. Most support it in one form or another.

I use ksh by preference, but I tested this with bash and everything should work fine.

HTH
User avatar
Garvan
Level 4
Level 4
Posts: 300
Joined: Sun May 29, 2011 3:26 am
Location: Thailand
Contact:

Re: How to automatically execute a command inside a certain

Post by Garvan »

I have a few questions on the syntax mph426 may be able to help with.

In the original .profile posted by xskyjackerx the modified PATH variable is not exported, yet this is the default script, and it appears to work. Is export needed?

Also I do not understand this:

alias cp="cp"
alias mv="mv"

What does alias do in this case? And again, what is the scope of these changes.

Thanks, Garvan
Notebook: DELL Latitude E5520, i5-2520M @ 2.50 GHz, 4GB RAM, Linux Mint 17.2 (2011)
Notebook: DELL Latitude 5280, i5-73000 @ 2.7 GHz., 16 GB RAM, Linux Mint 19.2 (2019)
xskyjackerx

Re: How to automatically execute a command inside a certain

Post by xskyjackerx »

Thank you so much guys.Im getting better with bash syntax now. :P
Locked

Return to “Scripts & Bash”