Add command line argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add command line argument
# 1  
Old 08-28-2018
Add command line argument

I would like to add the ability to change the message that is displayed when timer is finished. At present it just asks for the time I want for the alarm.

I think what I need is another command line argument.

Code:
soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"
originalVolume=$(amixer -D pulse get Master | grep -m 1 -o -E [[:digit:]]+%)
clear
amixer -D pulse sset Master 40% > /dev/null 2>&1
[ ! $1 ] && {
echo 
echo -e "   Error!! No time value given for sleep !!"
echo
echo -e "   Alarm Program 2018"
echo
echo -e "   alarm.sh [time value in seconds]"; 
echo -e "   alarm 5m   = 5 minute alarm"
echo -e "   alarm 5h   = 5 hour alarm"
echo -e "   alarm 5d   = 5 day alarm"
echo -e "   alarm 1.5m = 1 minute 30 seconds alarm"
echo 
exit 1; }
echo  -e "\033[32;5mTIMER COUNTING DOWN to $1 \033[0m"
sleep $1
{
    for ((volume = 15; volume <= 40; volume += 2)); do
        amixer -D pulse sset Master ${volume}% > /dev/null
        sleep .5
    done
} &

cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
echo 'TIME IS UP.'
gxmessage -fg blue -font  'sans 20' -timeout 2 ' TIME IS UP !!'

#set back to original volume
amixer -D pulse sset Master $originalVolume > /dev/null

# 2  
Old 08-28-2018
Yes - change ' TIME IS UP !!' to ${2:-Time is up\!}, taking advantage of "Parameter expansion - use default value". You may want to add some error checking for $2.
# 3  
Old 08-28-2018
Not quite what I need. I need to be able to enter something like

alarm.sh 5m Take bread out of oven.

$2 would be my message which can vary.

This works, but I have to enclose my message in double quotes.
test.sh 1 "Test print cartridge."

Code:
[ ! $2 ] && {
....
echo $2

# 4  
Old 08-29-2018
You might want to try something like:
Code:
case $# in
(0)	echo 'No time value given for sleep.'
	exit 1;;
(1)	time=$1
	note="Timer is counting down to $time."
	break;;
(*)	time=$1
	shift
	note=$*;;
esac
echo "time is $time"
echo "note is $note"

If you don't see what it is doing try invoking your script with:
Code:
bash -xv scriptname
bash -xv scriptname 15
bash -xv scriptname 30 Note is one or more command line operands.

# 5  
Old 08-29-2018
Thanks. How do I get this to show the double quotes ?

Code:
echo -e "   alarm.sh 1m "Take bread out of oven.""

# 6  
Old 08-29-2018
Three of hundreds of possible ways:
Code:
echo -e "   alarm.sh 1m \"Take bread out of oven.\""
echo -e '   alarm.sh 1m "Take bread out of oven."'
echo -e "   alarm.sh 1m 'Take bread out of oven.'"

You can't use the second form if the string you want printed between the quotes is the expansion of a shell variable.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command line argument

Hi Guys, I'm trying to work out how to add a command line argument inside single quotes. Would anyone be able to help please as I'm going mad :) I want to be able to place the filename on command line and it then be used in a script but it needs to have quotes surrounding it. Thanks in... (4 Replies)
Discussion started by: mutley2202
4 Replies

2. Shell Programming and Scripting

Checking has for a command line argument

I would like to search and print a match of the user entered $ARGV. Im terrible with hashes and really dont know where to go from here. The example data file name location phone Bugs holeintheground 5551212 Woody holeinthetree 6661313 Jerry holeinthewall 7771414... (4 Replies)
Discussion started by: sumguy
4 Replies

3. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

4. Shell Programming and Scripting

Help on command line argument in csh

HI , I am new to csh. I need to pass some command line arguments like ./abc.sh -os Linux -path abc -tl aa -PILX 1 I have defined the loop as shown below. But its taking "-os" switches as arguments. Its treating them as arguments. How to resolve it? while ( $#argv != 0 ) switch ($argv) ... (7 Replies)
Discussion started by: vineet.dhingra
7 Replies

5. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

6. Shell Programming and Scripting

finding * in command line argument

I have to write a script to determine whether given command line argument ($1) contains "*" symbol or not, if $1 does not contains "*" symbol add it to $1, otherwise show message "Symbol is not required". For e.g. If we called this script q5 then after giving , $ q5 /bin Here $1 is /bin, it... (5 Replies)
Discussion started by: cynosure2009
5 Replies

7. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. Shell Programming and Scripting

How to get the value in last command line argument???

Say I want to get the value of last command line argument using the value in $# (or some other way if u can suggest) how do I do it?? $"$#" `$"$#"` These don't work :( (4 Replies)
Discussion started by: amit_oddey21
4 Replies

10. UNIX for Dummies Questions & Answers

array as command line argument !!!!

hello, can any help me how to can pass array as command line argument in korn shell. also how to read a array from command line. thanks spandu (2 Replies)
Discussion started by: spandu
2 Replies
Login or Register to Ask a Question