I have a file called ~/.gotorc and in it I have
alias goto_usr="cd /usr"
alias goto_bin="cd /user/local/bin"
Then in my .zshrc I have
source ~/.gotorc
goto() {
`goto_$1`
}
But when I run goto bin, it says goto:bin: command not found: goto_bin. But if I run goto_bin then it works and I go to the expected folder.
How come?
Instead of aliases you can define a single function goto like this:
goto() {
case "$1" in
usr)
cd /usr;;
bin)
cd /usr/local/bin;;
esac
}
Then use it as:
goto usr
goto bin
Related
I am new to Batch scripting.
I am trying to write a script which parses given command and check if argument with name 'folder 'is present in that command and if not , add that argument with default value.
I have written following script. This scripts executes correctly if argument is missing.
But if argument is present , both if and else blocks are executed.
Please help. Thanks in advance.
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
Output of code :
Folder is specified in command
Folder is NOT specified in command. User's current directory will be used as Folder.
folderArgName : -folder
folderArgValue : "C:\work"
folderArg: -folder "C:\work"
ARGS : -action generate -folder "Source" -folder "C:\work"
You have to have a goto in the else, that goes to after the setDeafultFolder, otherwise it just will execute the setDefaultFolder after it
#echo off
set ARGS=-action generate -folder "Source"
set t=%ARGS%
echo %t%|find "-folder" >nul
if errorlevel 1 (
goto setDefaultFolder
) else (
echo Folder is specified in command
goto endOfBatch
)
:setDefaultFolder
echo Folder is NOT specified in command. User's current directory will be used as Folder.
set folderArgName=-folder
set folderArgValue="%cd%"
set folderArg=%folderArgName% %folderArgValue%
echo folderArgName : %folderArgName%
echo folderArgValue : %folderArgValue%
echo folderArg: %folderArg%
set ARGS=%ARGS% %folderArg%
echo ARGS : %ARGS%
:endOfBatch
In my .bashrc file, I have a function
changeDirectory()
{
cd "/home/bin"
}
If I'm in /home and I type changeDirectory on the command line, the current directory becomes /home/bin
If this function is called from other functions, it also works to change the current directory.
handler()
{
changeDirectory
}
If I type handler in the command line, it changes my current directory to /home/bin
However, if I call changeDirectory from this function:
command_not_found_handle()
{
changeDirectory
echo "$PWD"
}
When I type a command this isn't found, and this function is called, it will print /home/bin, but when I type pwd on the command line, I'm still in /home
The command_not_found_handler function seems to behave differently than normal functions. Is there a work around to be able to change my current directory?
A slight twist on #Cyrus' answer, using a temporary file instead of exit code.
command_not_found_handle () { touch $HOME/.changedirectory.$$; }
PROMPT_COMMAND='rm $HOME/.changedirectory.$$ 2>/dev/null && changeDirectory'
I have defined an alias like so:
alias X="path/to/program"
and I have a function defined like this:
doX() { X -flag "$1"; }
I put these in my .bashrc file, and when I open bash, I get a syntax error near unexpected token '-flag'. At this point, the alias has been set, but the function has not, due to this error. If I run
doX() { X -flag "$1"; }
at this point, it works. I have tried putting this into a file and sourcing it after I set the alias in the .bashrc file, but it is giving me the same results.
How can I fix this? Is there a way to define the alias AND the function in the .bashrc so that they are both set when I open bash?
Aliases are not usually available in scripts. If you want to have a function use an alias, consider making the alias itself a function:
X() { path/to/program "$#"; }
doX() { X -flag "$1"; }
I'm trying to call methods from file to file with structure like:
/root
/subDir
/subSubDir
inSubSub.sh
inSub.sh
inRoot.sh
Files contents:
inRoot.sh:
#!/bin/bash
source ./subDir/inSub.sh
subMethod;
inSub.sh:
#!/bin/bash
source ./subSubDir/inSubSub.sh
subMethod () {
echo "I'm in sub"
}
subSubMethod;
inSubSub.sh:
#!/bin/bash
subSubMethod () {
echo "I'm in subSub"
}
subSubMethod;
Result of running $ ./inRoot.sh
subDir/inSub.sh: line 2: subSubDir/inSubSub.sh: No such file or directory
subDir/inSub.sh: line 6: subSubMethod: command not found
I'm in sub
So, it works for the first call but doesn't work deeper.
btw: using . ./ instead of source ./ returns the same
How to do it right, if it's possible?
You must change your inSub.sh like that
cat ./subDir/inSub.sh
#!/bin/bash
var="${BASH_SOURCE[0]}"
source "${var%/*}"/subSubDir/inSubSub.sh
subMethod () {
echo "I'm in sub"
}
subSubMethod;
this script file name "1sr" and i can work in terminal ". 1sr"
i want to change directory "home/byram/workspace/1/src/com/seri/*"
#!bin/sh
f=$(basename $0 | tr -d "sr")
pth="/home/byram/workspace/$f"
my1=$(ls $pth/src/com/seri)
cd $etc/src/com/seri/$my1
after ". 1sr" command f variable set "bash"
how can i fix it?
I would suggest a function called "prj" to put in your .bashrc:
prj () {
cd /home/byram/workspace/"$1"/src/com/seri
}
Then use it like this
prj 1 # Switch to ...1/src/com/seri
prj 2 # Switch to ...2/src/com/seri
i add in .bashrc this lines:
wr (){
cd /home/byram/workspace/"$1"/w
v1=$(ls /home/byram/workspace/"$1"/src/*/*)
v2=$(ls /home/byram/workspace/"$1"/src/*)
v3=$(ls /home/byram/workspace/"$1"/src/)
echo "$v3.$v2.$v1"
}
works for any project eg. com.example.abc,org.samp.xyz
thanks for #chepner