Variable names in shell script [closed] - shell

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
This post was edited and submitted for review 4 days ago.
Improve this question
I am trying to write a shell script in the form of an executable file that echoes yes if I pass an argument that is valid variable name or no if it is not a valid variable name.
So for example ./valid abc would return yes and ./valid 123 would return no
As of right now, no matter what I write, I get yes a response. Also, form what I have learned so far, is that variable names cannot start with a number, which is why ./valid 123 should return no
This is the code I have so far:
name="$1"
if test "$name"=[a-zA-Z]+[a-zA-Z0-9_]*
then
echo "yes"
else
echo "no"
fi

Related

How do I extract particular name from a file name in shell [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
In shell scripting Suppose I have a file name called aaa_djdidm_cjcjcj_20220602.txt
I only want the name except the date.
Expected result - aaa_djdidm_cjcjcj
If you want to remove the part after the last _:
NAME="aaa_djdidm_cjcjcj_20220602.txt"
NEW_NAME=${NAME%_*}
The % operator is useful do remove the motif (here _*) of a named parameter (here NAME)

How to do arithmetic in bash when the operator is stored in a variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
add=+
echo "$((1{$add}2))"
If I write 1+2 it outputs 3, but when I store the + sign in a variable called "add" and then use it in place of + operator, it just outputs it as if it was a string. How can I use the variable and still make it output 3?
As far as I can see, the code is wrong and should not print anything but an error.
What you should do is write the variable as ${add}, not {$add}.

Reuse of the same variable wit using echo and cut [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm try to make a script for checking some ethx.
If the eth is up and connected my script is working
When its not working I have a problem.
RESD=$(ssh -q vmx#$1 cat /sys/class/net/$3/dormant)
I get in RESD the following result:
cat: /sys/class/net/eth3/dormant: Invalid argument
I want to put in RESD now the first letter.
How can I do this?
Thanks
As per the comment above, this what you want to do: RESD=${RESD:0:1}? This is pure bash equivalent of RESD=$(echo $RESD | cut -c1).

Alternate a BASH variable by Date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What method can one use to alternate a variable which is dependable by time?
For example, this week var = Office, then next week var = Oncall?
The resulting script will be using crontab to output the contents of var depending on which week it was.
Thanks.
You can use date +%W to check what week number of the year it is (starting Monday).
You can alternate between the two with e.g.
if (( $(date +%W) % 2 ))
then
var=Office
else
var=Oncall
fi

Unix: Trying to increment a calender year and display [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a unix assignment and What I have isnt working right. It doesnt format like the normal "cal" function.
#!/bin/bash
d=`date '+%Y'`;
$((++d));
calstr=`cal $d`;
echo $calstr;
You don't need the $ in line 2, and you need to wrap the $calstr in double quotes:
#!/bin/bash
d=`date '+%Y'`;
((++d));
calstr=`cal $d`;
echo "$calstr";

Resources