I am facing a problem with alias command in bash. I created .bashrc in my home directory and added an alias as below:
test() { "test_command_$1"; }
alias au=test
But when I open a new terminal and try to execute the command through alias like au arg1 it complains:
bash: test_command_arg1: command not found
But if I execute aliased command manually in the same terminal as test_command_arg1 it is working fine. I checked the PATH variable and it's fine. Can somebody help me to fix this?
P.S: test_command_ is just an example. It is not the actual command being tried.
You are getting the "command not found" error because of what your test() function does.
test() { "test_command_$1"; }
When you call this with an argument, $1 is replaced with that argument (I guess you knew this bit). So if you call au arg1, what your function is trying to do is this:
"test_command_arg1"
The shell is trying to execute it as a command. test_command_arg1 isn't a recognised command, so you get an error. Try changing your test function:
test() { echo "test_command_$1"; }
Related
I'm trying to customize the ssh command in ubuntu using the ~/.bash_aliases file, basically I want to use the command 'zssh |$value|' to do: ssh root#name|$valuegoeshere|.hostname.org, I have tried to use this code:
function zssh{ssh root#name$1.hostname.org}
However I got the following error:
bash: /home/amirs/.bash_aliases: line 1: syntax error near unexpected
token root#name$1.hostname.org}' bash: /home/amirs/.bash_aliases:
line 1:function zssh{ssh root#name$1.hostname.org}'
Any suggestions on how to configure the following function?
The correct way to define a function is:
zssh () {
ssh root#name$1.hostname.org
}
The function keyword is optional and a bash extension, so there's no need to use it. You need whitespace around the { character, and there has to be a command delimiter (either newline or ;) before }.
I'm about to learn bash scripting and wrote a little script like this for testing purposes:
#!/bin/bash
function time {
echo $(date)
}
time
However the function doesn't get executed, instead the command time is running.
So what do I have to do to execute the function instead?
I'm running bash 4.2.45
To run a function with the same name as the special keyword time, quote it e.g.:
function time {
echo "$(date)"
}
'time'
You need to add () to the function definition. I'm not sure you need the function text there
Following should work:
#!/bin/bash
get_time() {
echo $(date)
}
get_time
Edited: time seems to be a reserved keyword, so changed the function name
I am using nedit to edit source code in my workstation. Yet, it starts with the following errors:
Cannot convert string "-*-helvetica-medium-r-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Cannot convert string "-*-helvetica-bold-r-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Cannot convert string "-*-helvetica-medium-o-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Cannot convert string "-*-courier-medium-r-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Cannot convert string "-*-courier-bold-r-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Cannot convert string "-*-courier-medium-o-normal-*-*-120-*-*-*-iso8859-1" to type FontStruct
Not knowing how to fix these errors, I used an alias to start edit:
ne='nedit &>/dev/null &'
It is to suppress the warning messages spit to stdout and stderr, and let nedit run in the background, so that I can type the next command in the current terminal window.
Yet, if I use this alias to open a file directly, it gives me an error msg like:
[qxu#merlin:/home/qxu/work/src]# ne abc.c
[4] 24969304
-bash: ./abc.c: The file access permissions do not allow the specified action.
Yet, nedit abc.c works, though with the above font error msgs.
Is there a way for me to use the above alias and give it a filename to open directly?
Use a function instead of an alias. When we have to handle arguments, it is more simple to use. Place the following function in your .bashrc file:
function ne() {
command nedit "$#" &>/dev/null &
}
In this example, when you run ne file.txt, you call this function which executes the command nedit with all the arguments you've passed ("$#").
Take a look at this explanation of when you should use an alias or a function. It is very good
The problem with your alias is that you have & in the wrong place. When the alias is expanded you get
nedit &>/dev/null & abc.c
& is a command separator, so this is equivalent to
nedit &>/dev/null & # launch nedit in the background without a file to edit
abc.c # execute abc.c
and apparently "abc.c" does not have execute permissions.
As Victor said, use a function.
For some reason, this function is working properly. The terminal is outputting
newbootstrap.sh: 2: Syntax error: "(" unexpected
Here is my code (line 2 is function MoveToTarget() {)
#!/bin/bash
function MoveToTarget() {
# This takes two arguments: source and target
cp -r -f "$1" "$2"
rm -r -f "$1"
}
function WaitForProcessToEnd() {
# This takes one argument. The PID to wait for
# Unlike the AutoIt version, this sleeps for one second
while [ $(kill -0 "$1") ]; do
sleep 1
done
}
function RunApplication() {
# This takes one application, the path to the thing to execute
exec "$1"
}
# Our main code block
pid="$1"
SourcePath="$2"
DestPath="$3"
ToExecute="$4"
WaitForProcessToEnd $pid
MoveToTarget $SourcePath, $DestPath
RunApplication $ToExecute
exit
You're using the wrong syntax to declare functions. Use this instead:
MoveToTarget() {
# Function
}
Or this:
function MoveToTarget {
# function
}
But not both.
Also, I see that later on you use commas to separate arguments (MoveToTarget $SourcePath, $DestPath). That is also a problem. Bash uses spaces to separate arguments, not commas. Remove the comma and you should be golden.
I'm also new to defining functions in Bash scripts. I'm using a Bash of version 4.3.11(1):-release (x86_64-pc-linux-gnu) on UbuntuĀ 14.04 (Trusty Tahr).
I don't know why, but the definition that starts with the keyword function never works for me.
A definition like the following
function check_and_start {
echo Hello
}
produces the error message:
Syntax error: "}" unexpected
If I put the { on a new line like:
function my_function
{
echo Hello.
}
It prints a Hello. when I run the script, even if I don't call this function at all, which is also what we want.
I don't know why this wouldn't work, because I also looked at many tutorials and they all put the open curly brace at the end of the first line. Maybe it's the version of Bash that we use?? Anyway, just put it here for your information.
I have to use the C-style function definition:
check_and_start() {
echo $1
}
check_and_start World!
check_and_start Hello,\ World!
and it works as expected.
If you encounter "Syntax error: "(" unexpected", then use "bash" instead of using "sh".
For example:
bash install.sh
I had the same issue. I was running scripts on Ubuntu sometimes using sh vs. Dash. It seems running scripts with sh causes the issue, but running scripts with Dash works fine.
I want the PHP equivalent of the solution given in assigning value to shell variable using a function return value from Python
In my php file, I read some constant values like this:-
$neededConstants = array("BASE_PATH","db_host","db_name","db_user","db_pass");
foreach($neededConstants as $each)
{
print constant($each);
}
And in my shell script I have this code so far:-
function getConfigVals()
{
php $PWD'/developer.php'
//How to collect the constant values here??
#echo "done - "$PWD'/admin_back/developer/developer.php'
}
cd ..
PROJECT_ROOT=$PWD
cd developer
# func1 parameters: a b
getConfigVals
I am able to execute the file through shell correctly.
To read further on what I am trying to do please check Cleanest way to read config settings from PHP file and upload entire project code using shell script
Updates
Corrected configs=getConfigVals replaced with getConfigVals
Solution
As answered by Fritschy, it works with this modification:-
PHP code -
function getConfigVals()
{
php $PWD'/developer.php'
#return $collected
#echo "done - "$PWD'/admin_back/developer/developer.php'
}
shell code -
result=$(getConfigVals)
echo $result
You have to execute the function and assign what is printed to that variable:
configs=$(getConfigVals)
See the manpage of that shell on expansion for more ;)