tcsh shell pass arguments with quotes and * to alias - alias

1.I'm trying to alias my "find" command in tcsh shell at the end I want it to look like this:
find path file_name
instead of: find path -name "*file name*"
path is the path for find, and file name is part of the file name.
I tried this:
alias fn 'find \!:1 -name "*\!:2*" '
and it doesn't work. any idea why?
How can I print the command before/after it's execution? I want to see what's really running under the hood after I send an alias command - see what is the final command was executed.

You musn't be afraid to dream a little bigger, darling.
> alias fn 'echo find \!:1 -name \"\*\!:2\*\"; eval find \!:1 -name \"\*\!:2\*\"'

Related

Jenkins variable not working with sed command in pipeline

The Sed command is giving me issues with incorporating the $tag variable witch is equal to "latest${GIT_COMMIT:0:7}". Here is the Sed command:
sh "sed -i 's/{BUILD_NUMBER}/$tag/' /var/lib/jenkins/workspace/${JOB_NAME}/em-api/dev-nics-emapi-svc-param.json"
I obviously want to put into my .json file the commit information but It doesnt pull the actual commit sha. When I take a look at the .json file it inserted the literal definition of the variable which is “latest${GIT_COMMIT:0:4}”. I am trying to do this on a declarative pipeline on my jenkins server running on linux.
I would like it to insert "latestxxxx". Any suggestions on how I can get around this?
GIT_COMMIT is an environment variable available to you; tag is a groovy variable, you have set to 'latest${GIT_COMMIT:0:4}'. So this gets replaced since you are using " for your sed command. But you are using ' for your sed expression, which then again will not replace environment variables. So you have basically two options:
Use " to quote the sed command, if you feel safe about the content, that gets replaced (you can use """ triple quotes for the whole command to don't have to quote the " for groovy)
Resolve the variable from the environment yourself in groovy (e.g. something like System.env['GIT_COMMIT].substring(0,4))

How can I use source script with variables in CygWin?

I'm trying to use external script with variables, but in result I get only "no such file or directory".
1st.ksh
#!bin/ksh
PATHNAME = `dirname $0`
. $PATHNAME/2nd.ksh
Echo $EXTVAR
2nd.ksh
#!bin/ksh
EXTVAR=1
I tried to use "Source" instead of "." (Source $PATHNAME/2nd.ksh) and I get the same result.
To run script I'm using full path to the script - cygdrive/e/Folder/1st.ksh.
2nd.ksh in this path too (cygdrive/e/Folder/).
All rights was granted for both files (chmod u=rwx,g=rwx,o=rwx filename).
If I put files in cygwin home path (/home/username/) I have the same.
Please help to understand what I'm doing wrong.
Thanks in advance!
$() should be used in ksh instead of `` (link)
. should be user instead of source (link)
"=" must not be surrounded with spaces. You should write: PATHNAME=$(dirname $0)
you should be aware of case-sensitiveness: echo, source

An alias that just prints the named directory?

I'm kind of just realizing how powerful the terminal can be. My question is essentially if I can create an alias that just prints the name of a directory. For example, I could easily make an alias such as "alias sitename="cd ~/sites/path/to/my/site/". But what I want is an alias that only prints the directory name so that I can use it for several things. So that, for example, if I wanted I could just say cd "alias", or mv from-dir "alias".
Is there a way to do this? I've tried and it seems to recognize the alias if I just type it in: it will report "alias" is a directory. But if I try to couple it with another command, it fails.
You don't want to use alias, what you are after is an environment variable
$ export SITENAME="~/sites/path/to/my/site/"
$ cd $SITENAME
Bash is quite picky over syntax - note the lack of spaces in the export and the $ when you use it.
Use a variable, simply
d=/path/to/some/directory
echo $d
cd $d
mv somedir $d/
You don't need to use an alias here, a variable is sufficient.
It sounds like you want to set a variable, not an alias. Such as,
sitename=/home/jimbo/. Then, cd $sitename would put you at /home/jimbo/.
If you want this variable to have permanence (i.e. you don't have to set it every time you open a new session), then you can make it an environmental variable using the export command or add it to your .bashrc file (typically located at $HOME/.bashrc) using the line: sitename=/home/jimbo/.
FYI, $HOME is another environmental variable that's equivalent to ~/.
Using a variable is the simplest solution. You could get fancy and use an array:
mydir() { echo "/my/directory"; }
To display the value
mydir
To use the value, you need some extra puncuation
cd $(mydir)
cd `mydir`

Using an environment variable in a PSQL script

Is it possible to use a Linux environment variable inside a .sql file? I'm using the copy/select query to write to an output file, and I'll like to put that directory in a variable. So I want to do something like:
COPY (SELECT * FROM a)
TO $outputdir/a.csv
Outputdir would be set in my environment. Is this possible?
You can store the result of a shell command inside a psql variable like this:
\set afile `echo "$outputdir/a.csv"`
COPY (SELECT * FROM a) TO :'afile';
Another (better in my opinion) solution is to use only psql variables, see this answer of mine about psql variables, which is similar to your example. A example for your case would be:
\set outputdir '/path/to/output'
\set afile :outputdir '/a.csv'
COPY (SELECT * FROM a) TO :'afile';
Note that, in the example, you need to set the variable inside the script file, but you can skip the first line if you set it when you call psql:
psql --set=outputdir="$outputdir" <conn parameters> -f /path/to/yourscript.sql
This appears to work for your use case, provided you single quote the output file name as I mentioned. It will escape any double quotes as well contained within the SQL.
psql -c "$(eval echo '"' $(<envvars.sql | sed 's/"/\\"/g') '"')"
Of course, note that if your file contains any dollar quoted variables, the shell is going to try to interpret as a variable, and your script will break, so you will need to escape any dollar signs you need preserved literally with a backslash.
See also the second snippet in the accepted answer to this question for a possibly more robust answer.
The accepted answer is correct for PostgreSQL running on Unix. Under Windows a different incantation is required for obtaining the value of the environment variable from the CMD shell and for avoiding the carriage return returned by the echo command.
\set afile `set /p=%outputdir%/a.csv`
COPY (SELECT * FROM a) TO :'afile';

How to get fully expanded variables out of configure?

I created a configure.ac file like this:
AC_INIT()
set
the purpose of this is to print every available environment variable the configure script creates using set, so I do this:
user#host:~$ autoconf
user#host:~$ ./configure
which prints a bunch of variables like
build=
cache_file=/dev/null
IFS='
'
LANG=C
LANGUAGE=C
datarootdir='${prefix}/share'
mandir='${datarootdir}/man'
no_create=
So far so good.
The problem is:
I want to expand the variables like ${prefix}/share - but piping
everything to a file example.sh and executing it using bash doesn't work, because bash complains about modifying read-only variables like UID and expansion itself doesn't seem to work either.
I tried using a makefile for this where expansion works, but it complains about newlines in strings, like in the above output the line IFS=' causes an error message Makefile:24: *** missing separator. Stop.
Does anyone have an idea how to get a fully expanded version of configure's output?
The Autoconf manual (I cannot recall or find exactly where) recommends to "manually" do such a kind of variable substitution from within a Makefile.in (or .am if you happen to use Automake):
Makefile.in
[...]
foo.sh: foo.sh.in
$(SED) \
-e 's|[#]prefix#|$(prefix)|g' \
-e 's|[#]exec_prefix#|$(exec_prefix)|g' \
-e 's|[#]bindir#|$(bindir)|g' \
-e 's|[#]datarootdir#|$(datarootdir)|g' \
< "$<" > "$#"
[...]
foo.sh.in
#!/bin/sh
datarootdir=#datarootdir#
du "$datarootdir"