LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2009, 11:15 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
help me in my bash script


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.

Ant advise?
 
Old 12-13-2009, 11:22 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
len=$#
user="$1@foo.com"
case "$len" in
    1) echo "TEST1";;
    2) echo -n " TEST2 #> /var/log/test2
esac
 
Old 12-13-2009, 11:30 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
thanks for the idea! I'll let you know if I still encounter some problem.
 
Old 12-14-2009, 12:23 AM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Quote:
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?
 
Old 12-14-2009, 12:35 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
then you should use quotes
Code:
./script user "hello world"
otherwise, you can iterate over $@ starting from 2,
Code:
save=("$@")
user=${save[0]}
s=${save[1]}
for((i=2;i<=${#save[@]};i++))
do
    s="$s ${save[$i]}"
done
echo "user: $user, the rest:$s"
or using array indexing
Code:
save=("$@")
user=${save[0]}
s=${save[@]:1}
echo "user: $user, the rest:$s"

Last edited by ghostdog74; 12-14-2009 at 12:55 AM.
 
Old 12-19-2009, 03:27 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
here's a suggestion:
Code:
echo "user: $1"
echo "message: ${*:2}"
note: result of ${*:2} will depend on the value of IFS. it's safe if IFS has the default value $' \t\n'

Last edited by konsolebox; 12-19-2009 at 03:28 AM.
 
Old 12-19-2009, 04:18 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
echo "user: $1:
shift
echo "message: $*"
 
Old 12-19-2009, 04:31 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by catkin View Post
Code:
echo "user: $1:
shift
echo "message: $*"
i'll agree that this is better since it's more compatible with other shells
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration