ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I create a bash script with using case syntax. My bash script option must be "./script user hello". Below is the content of the script.
Quote:
user=`echo "$1" | sed "s/$/@foo.com/"`
case $1 in
*)
echo "From: $user
TEST1" > /var/log/test1
echo -n " TEST2" > /var/log/test2
;;
esac
I want to modify it and do something like this.
If I run the script ./script user (without the word "hello"), it will echo -n " TEST2" > /var/log/test2, if I run the script ./script user hello, it will return echo -n " TEST1". I'm thinking if I can do this using if syntax though I'm not sure and still checking if can do this.
len=$#
user="$1@foo.com"
case "$len" in
1) echo "TEST1";;
2) echo -n " TEST2 #> /var/log/test2
esac
I encountered some problem here. I gonna run the script like this: ./script user hello word. On the script, 1 = user and 2 = hello, what if there are more than 2? I want to make the first 1 a variable and then any succeeding world must be equal to 2.
Here is some of my modified script
Quote:
USER=$#
case $USER in
2)
echo -n "$1"
;;
1)
echo -n "$2"
;;
esac
If I run the script ./script user hello world, I want it to echo "hello world" and not just "hello".
Can still bash do this? Any syntax you might want to suggest to fulfill this?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.