How to store the output of a command into a variable in TCSH? - tcsh

in bash this clearly works
$ now=$(date "+%F")
$ echo $now
2022-07-20
but in tcsh:
>> now=$(date "+%F")
Illegal variable name.
Is there a way to store such a thing into a var?

csh doesn't support $() for command execution, only backticks. And you need to use set to assign variables.
% set now = `date "+%F"`
% echo $now
2022-07-20

Related

Store last command in a variable in Bash

I'm want to store the most recent command in a variable. I tried the !!:p history expansion, it does get me the last command but I can't store it in a variable.
$ last=`!!:p`
last=`ls`
$ echo $last
$
Any help?
The fc command can be used to retrieve the previous command.
some_var=$(fc -nl -1)
Using !!:p only prints the last command, to execute the last command you would do !!.
$ mycmd="$(!!)"
$ sh "$mycmd"
That should do what you're looking for...
The solution given above does not work inside a script file:
some_var=$(fc -nl -1)
What I did was use trap option and store the last command in environment variable before the script is called and access the env variable in script.
Add this function in ~/.bashrc
function process_command() {
if [ "$BASH_COMMAND" != "$PROMPT_COMMAND" ];then
export myCMD=$BASH_COMMAND
fi
}
trap process_command DEBUG
Then use $myCMD variable in any script or anywhere in shell.

How to pass shell variables in "echo" command

I have she script as the below content
chr=$0
start=$1
end=$2
echo -e "$chr\t$start\t$end" > covdb_input.bed
How do i pass the chr,Start and end variables in to echo command.. or write same to file "covdb_input.bed" with TAB sep as in echo command.
You're doing everything right, except that you probably initialize your variables with the wrong things.
I'm assuming you get arguments for the script (or shell function), and that you want to use these. Then pick the positional variables from $1 and onwards as $0 will usually contain the name of the current shell script or shell function.
Also, you might find people scoffing about the use of -e with echo (it's a common but non-standard option). Instead of using echo you could use printf like this:
printf "%s\t%s\t%s" "$chr" "$start" "$end" >myfile.bed
Or just
printf "$chr\t$start\t$end" >myfile.bed

Shell script with comment, command not found when creating variable

I have the following script which works:
x=10
echo $x
now=$(date +'%Y-%m-%d')
echo $now
However, when I add a comment line at the beginning:
# comment
x=10
echo $x
now=$(date +'%Y-%m-%d')
echo $now
I get the following:
x=10: command not found
x: undefined variable
Why is the addition of the comment causing the script to fail?
if I do the following it works:
x=10
echo $x
now=$(date +'%Y-%m-%d')
# comment here
echo $now
This is a quirk of csh. (Stop using csh!) csh will process a script that does not begin with a '#' using a "standard shell" (quoting from the csh manpage.) When the script begins with '#', csh parses it. Your script is not valid csh.
You should probably add a shebang line to avoid this type of issue. That is, make the first line:
#!/bin/sh
try something like this
#!/bin/sh
#
x=10
echo $x
now=$(date +'%Y-%m-%d')
echo $now
This works on my system (Ubuntu 11.04, 64bit). If that doesn't work then you may have some hidden special character in your file.

"let" internal shell command doesn't work in a shell script?

I did
a=1234
let "a=a+1"
on command line and it's fine. But when I do the same in a shell script. It prints out an error that "let: not found".
Here is the script file.
#!/bin/sh
a=1234;
let "a=a+1";
echo "$a";
Thanks,
Do not use let. Use POSIX arithmetic expansion: a=$(($a+1)). This is guaranteed to work in any POSIX-compliant shell.
The problem is likely that /bin/sh is not the same as, or does not behave the same as, your normal shell. For example, when bash is invoked as /bin/sh, it provides a subset of its normal features.
So, you may need to change your shebang line to use a different shell:
#!/bin/bash
or
#!/bin/ksh
You don't need the semi-colons at the ends of the lines.
See at: http://www.hlevkin.com/Shell_progr/hellobash.htm
The correct is:
a=1234;
b=1;
a=`expr $a + $b`;
You should use let a=a+1 without quotes
It's the '$a' of '$a=1234' that is killing you.
The shell does all $ substitutions and THEN evaluates the expression. As a result it saw "=1234" because there was no value to $a.
Use -x to see this.
bash -x your-script
Check your actual shell with the following command in the command line:
echo $SHELL
It will provide a shell name, use that instead of /bin/sh at the first line of your script.
Check the shell name using
echo $SHELL
Change the first line of the script accordingly to
#!/bin/bash
or
#!/bin/ksh
instead of #!/bin/sh.
c=1
d=1
for i in `ls`
do
if [ -f $i ]
then
echo "$c -> $i"
c=`expr $c + 1`
fi
done
c=`expr $c - 1`
echo no. of files $c
for i in `ls`
do
if [ -d $i ]
then
echo "$d -> $i"
d=`expr $d + 1`
fi
done
d=`expr $d - 1`
echo no. of direcrories $d

How can you get a variable's value given its name in korn shell?

Is there a way in ksh to get a variable's value when you have been given the name of the variable?
For example:
#!/usr/bin/ksh
var_name=$1 #pretend here that the user passed the string "PATH"
echo ${$var_name} #echo value of $PATH -- what do I do here?
eval `echo '$'$var_name`
echo concatenates a '$' to the variable name inside $var_name, eval evaluates it to show the value.
EDIT:
The above isn't quite right. The correct answer is with no backticks.
eval echo '$'$var_name
printenv is not a ksh builtin and may not always be present. For older ksh versions, prior to ksh93, the eval 'expression' method works best.
A powerful method in ksh93 is to use indirection variables
with 'nameref' or 'typeset -n'.
Define and verify a nameref variable that refers to $PATH:
$ nameref indirect=PATH
$ print $indirect
/usr/bin:/usr/sbin
See how the nameref variable changes when we change PATH:
$ PATH=/usr/bin:/usr/sbin:/usr/local/bin
$ print $indirect
/usr/bin:/usr/sbin:/usr/local/bin
Show ksh version and the alias for nameref:
$ type nameref
nameref is an alias for 'typeset -n'
$ echo ${.sh.version}
Version JM 93t+ 2010-02-02
var_name=$1 #pretend here that the user passed the string "PATH"
printenv $var_name
For one step above your answer (I spent a lot of time trying to find both these answers). The below will allow you to export a dynamic variable and then recall it dynamically:
echo -n "Please provide short name for path:"
read PATH_SHORTCUT
echo -n "Please provide path:"
read PATH
eval export \${PATH_SHORTCUT}_PATH="${PATH}"
eval echo Path shortcut: ${PATH_SHORTCUT} set to \$"${PATH_SHORTCUT}_PATH".

Resources