Bash Script Issues (If statement for file copying)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script Issues (If statement for file copying)
# 1  
Old 06-30-2012
Bash Script Issues (If statement for file copying)

Writing a bash script for use with Geektool, pulls the battery info, and shuffles images around so that an Image geeklet can display the correct expression as the desktop background. (Eventually I intend to make it more intricate, based on more variables, and add more expressions)

I'm extremely new to the language, but the script below did return a result the first time I ran it through Geektool. The only problem is that the variable did not match the result (batt at 95%, result was for 5% and below). It stopped working after that.

The percentage display works every time, but that's most likely because I didn't write that part of the script, I scavenged it. I'd rather use a variable than rerun the calculation over and over, but I couldn't get it to work.






Code:
#!/bin/bash

asbreg=`ioreg -rc "AppleSmartBattery"`

maxcap=`echo "${asbreg}" | awk '/MaxCapacity/{print $3}'`;
curcap=`echo "${asbreg}" | awk '/CurrentCapacity/{print $3}'`;

prcnt=`echo "scale=2; 100*$curcap/$maxcap" | bc`;

if [ 100*$curcap/$maxcap -gt 15 ] ; then

cp /Users/User/Desktop/Faces/Happy.png /Users/User/Desktop/Faces/Current/Current.png

elif [ 100*$curcap/$maxcap -gt 10 ] ; then

	cp /Users/User/Desktop/Faces/Queasy.png /Users/User/Desktop/Faces/	Current/Current.png

	elif [ 100*$curcap/$maxcap -gt 5 ] ; then

		cp /Users/User/Desktop/Faces/Angry.png /Users/User/Desktop/Faces/Current/Current.png

		elif [ 100*$curcap/$maxcap -gt 1 ] ; then 

			cp /Users/User/Desktop/Faces/Unconcious.png /Users/User/Desktop/Faces/Current/Current.png

fi

printf "%1.0f%%" ${prcnt};


#EOF

# 2  
Old 07-01-2012
In bash you need to pre-evaluate mathematical expressions. eg.

Code:
if [ $(( 100 * $curcap / $maxcap )) -gt 15 ] ; then

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to append array value to file before copying

The bash stores each uniqueid in an array and then passes them to %q to get the unique path. That seems to work what I am having trouble with is renaming each .png with the unique value in %q. I thought it was working but upon closer inspection, a .png file is being sent to scp.... but only 1 and... (21 Replies)
Discussion started by: cmccabe
21 Replies

2. Shell Programming and Scripting

Need help in finding and copying list of files using bash shell script

Dear All, I have a situation where I want to copy some files of type .txt. These files are o/p from one program. Some of the files are named as fileName .txt instead of fileName.txt after fileName by mistake I have specified "space". Now I want to move these files as follows. mv fileName*... (13 Replies)
Discussion started by: linuxUser_
13 Replies

3. Shell Programming and Scripting

Copying large files in a bash script stops execution

Hello, I'm new to this forum and like to first of all say hello to everyone. I've got a really annoying problem at the moment. I'm trying to rsync some files (about 200MB with one file of 120MB) from a Raspberry PI with raspbian to a debian server via rsync. This procedure is stored in a... (3 Replies)
Discussion started by: wex_storm
3 Replies

4. Shell Programming and Scripting

Unix bash script (mv issues);

Hey guys, I've registered here as I need urgent help. This is assignment for school and as you can see below I've completed the work. I'm simply stuck on one area. :wall: This script takes the first parameter (which is to be the new extension) and each parameter after that is a file... (1 Reply)
Discussion started by: Cynosure
1 Replies

5. Shell Programming and Scripting

Bash: renaming file issues.

Hi, i want to rename a group of directories and files of my music, some items are like this: - , for directories. - , for files. I want to do something like this: , for directories. , for files. This is my code: #!/bin/bash for fname in *.mp3; do echo item: $fname mv... (2 Replies)
Discussion started by: josco1982
2 Replies

6. Shell Programming and Scripting

Bash Script issues

So what i am trying to do is write a script that takes in any number of scrambeled words and unscrambles them. I have already addressed the issues of partial matches, exits silently if there are no matches, and finds words regardless of case that they were input. but while trying to get it so... (3 Replies)
Discussion started by: alindner
3 Replies

7. Shell Programming and Scripting

Noob's 1st...bash-script for copying one file into many

I have one file "file.a.b.c-d.r" that I would like to use to spawn 4 other files: "file.a.b.1-A.r" "file.a.b.1-B.r" "file.a.b.1-C.r" "file.a.b.1-D.r" where the field "c-d" changes into my 1 and A-D. I was doing this manually at the prompt with > cp "file.a.b.c-d.r" "file.a.b.1-A.r" >... (13 Replies)
Discussion started by: WSUToad
13 Replies

8. Shell Programming and Scripting

Bash script issues

Hi. The below part of my bash script kicks out the following error message: $ ./extract_eod_report_stats_new.sh 2010-04-23 ./extract_eod_report_stats_new.sh: line 204: syntax error near unexpected token `(' ./extract_eod_report_stats_new.sh: line 204: `TRANSACTIONS_RECEIVED_TOP=`grep... (6 Replies)
Discussion started by: Peter.Lovell
6 Replies

9. UNIX for Dummies Questions & Answers

IP connection Bash script issues

Hello I need assistance with a bash script that needs to tell me whether in the last "x" days (which is a variable) anyone connected to the xxxx IP (which is another variable). Thank you! (1 Reply)
Discussion started by: Melchiah
1 Replies

10. Solaris

Issues with copying using rsh

Hi, I'm having a few issues with rsh. I'm trying to copy data from one host to another using tar with the following command: tar -cf - /data | rsh xx.xx.xx.xx tar -xf - I'm copying the data from an X4100 to an X4140 server and have done this before with no issues. But with these... (1 Reply)
Discussion started by: Chains
1 Replies
Login or Register to Ask a Question