Programming :: Arithmetic In Shell Scripts?
Nov 19, 2010Is it possible to do arithmetic in shell scripts? If so, just how is it done?Here is the code so far:
Code:
#!/bin/bash
time=$(date +%H%M)
[code]...
Is it possible to do arithmetic in shell scripts? If so, just how is it done?Here is the code so far:
Code:
#!/bin/bash
time=$(date +%H%M)
[code]...
I have four files that contain numbers that I grep for in a variable#!/bin/bashcat FILENAME`date +%y%m%d*.tot` | grep Grand | awk '{print $4}'
#output is
12
67
[code]...
What is the difference between *ptr++ and (*ptr)++. In my opinion the terminology is all over the place so if you could give an example it would be better.
View 8 Replies View RelatedI do not understand bash' syntax regarding the use of ((<arithmetic expression>)). $((<arithmetic expression>)) is tokenised to a single word. OK.
Code:
In isolation
Code:
It may not be used where a word is expected. This generates a syntax error. Why?
Code:
My main problem right now is doing floating point arithmetic within a bash script, with variables.Right now I have a folder called "myExamples" with a script called "run_example" that runs with no issues.I plan to(1) create many folders inside [myExamples], that are named [example10] [example11]...each containing an identical copy of (run_example),(2) modify Line 172 of each copy of (run_example)...in one copy, it would be 3.00, the next copy would have 3.05, etc. (This part doesn't work!)How can use the available calculator bc code to do floating point operations?My code is below -
#!/bin/sh
# run from directory where this script is
cd `echo $0 | sed 's/(.*)/.*/1/'` # extract pathname
[code]...
I am trying to create a shell script similar to ls, but which only lists directories. I have the first half working (no argument version), but trying to make it accept an argument, I am failing. My logic is sound I think, but I'm missing something on the syntax.
Code:
if [ $# -eq 0 ] ; then
d=`pwd`
for i in * ; do
if test -d $d/$i ; then
echo "$i:"
code....
Is there some type of functional way to read things in the Python shell interpreter similar to less or more in the bash (and other) command line shells?
Example:
Code:
>>> import subprocess
>>> help(subprocess)
...
[pages of stuff to read]
...
I'm hoping so as I hate scrolling and love how less works with simple keystrokes for page-up/page-down/searching etc.
I've created a simple script based menu. This menu will be accessed by only a certain users via ssh.When user logs in, the menu will automatically run. (configured at user's .bash_profile).How do I force the session to close when user hits Ctrl-C or Ctrl-Break ?In a nutshell, I don't want user to have access to shell.
View 11 Replies View RelatedI've the following input file (sorted.txt)
Code:
A 1
A 1
[code]...
I'm trying to make a bash script that takes away two decimal numbers. This is to work out partitioning information.
Code:
hddsize=`sudo parted -s /dev/sda unit GB print | grep "Disk" | tr -d [A-Z][a-z] | tr -d '/: '`
trimsize="1.5"
partitionendsize=$(($hddsize - $trimsize))
I get an invalid arithmetic operator. Not sure how to minus these two numbers. Is it a floating point issue?
my host is x86_64 and run hosted redhat server5.4 X86_64, i complies my code with -m32 option open, my code run smoothly in my host, but when it installed on redhat9.0 host(x86_32) the error occurs and the output is :"Program terminated with signal SIGFPE, Arithmetic exception" but thought whole my code, there no divide operators,i write the "hello world" program, the same problem also occurs
View 1 Replies View RelatedI recently started shell programming and my task now is to do a menu display.Currently i am stuck whereby user will input both title and author and it will delete it.
Do i have to use sed command?
Is it possible to assist in the programming Shell Scripts
Job:
To send a message to the email,All orders written in Terminal or ssh
example : ls , pwd , cat , and other
I have a situation where I am in a non-interactive shell. I have tried from within my non-interative shell to spawn an interactive shell but my output still does not goto me. Isn't there a way I can somehow go into /proc or somwhere and make the output my /dev/tty1? Or some way else to remedy this?The situation arises because I drop from my restricted shell environment (a sort of CLI interface), into the actual Linux shell. I cannot change the code of the CLI environment I am just faced with being in the linux shell environment and its non-interactive. Its very annoying to have to put > /dev/tty1 after every command I type.
Not to mention it seems damn near impossible to get pagers like more and less to work properly when your in a non-interactive shell.
I wonder how to arithmetic operation of our data file using any script program/command (like sed, cut,awk)for example my data.file is:
45 65 42
12 23 61
43 34 21
I want to multiply first column with 2 and add second column with 3 then it become
90 68 42
24 26 61
86 37 21
I have a shell script that I would like to log to stdout and also to a file.....much like using tee. I would like to, instead of calling the script and piping to tee...i would like for the script to tee itself.
View 4 Replies View Relatedi got to build my own linux shell for my project
View 5 Replies View RelatedI have been searching most of today and am stuck on getting a variable into an awk portion of my bash script. I have this working:
Code:
#!/bin/sh
SRC=/var/log/mail.log
DEST=/var/www/output/myFile.txt
VAR=userName@myDomain.tld
[code]....
Can awk take a shell variable? Or do I have to do something completely different?
I need to push out a file to a bunch of linux and solaris boxes so I was hoping to use a script to automate the process with scp (or something else / better) - how can I batchmode scp so that it will do this? I should mention the servers do have keys set up so I do not have to authenticate manually or through something like an expect script...
View 4 Replies View RelatedI am having a shell script with following lines
ORIG_LOG_ARCHIVE_FORMAT=`grep log_archive_format $LOG_DIR/init$CLONE_DB.ora | cut -d"=" -f2`
SRC_LOG_ARCHIVE_FORMAT=`sqlplus -s "/ as sysdba" @$SCRIPTS_DIR/log_archive_format.sql`
echo $ORIG_LOG_ARCHIVE_FORMAT
[code]...
When I execute this script I am getting below output
'enin16_%t_%s_%r.arc'
enip131_%t_%s_%r.arc
sed: command garbled: s/'enin16_%t_%s_%r.arc'/
However when I do the exact above command on unix prompt (refer below) after setting values for all variables, it is not giving sed garbled error message. May I know what should I do to make it work inside the script as well?
$ cat init_refresh$CLONE_DB.ora | sed "s/$ORIG_LOG_ARCHIVE_FORMAT/$SRC_LOG_ARCHIVE_FORMAT/g" > $LOG_DIR/init_refresh1$CLONE_DB.ora
$
I'm trying to write a toy linux shell. For starters this is what I'm trying to do:
1. Start a new process with fork.
2. Execute a program in the new process with execl().
3. Redirect the output from the new process from STDOUT to another file descriptor, using dup2(2).
4. In the parent process, read the output from the child process and write it to the screen.
Creating a new process and executing a program in it is no problems, the problem is that I can't seem to capture the output from it in the parent process.
Code:
//digenv
//C++arl 2011-03-24
#include <stdio.h>
#include <sys/types.h>
[code]...
i write this script:
[Code].....
i make this script to become a service ( i test it from bash, and my script work good), my problem is when i telnet to my local host (127.0.0.1) via a personal port like 5555 telnet give this error: ")syntax error: invalid arithmetic operator (error token is ", i know this error is for echo $(($A+$B)) and i know that telnet can not calculate $A+$B and the error is for this
How to use shell command?
installs.sh:
Code: Select allapt-get install icedove-l10n-hu
apt-get install rar
apt-get install ...
...
y press key or other language is other key. hungarian key is: i
english after apt-get install in gnome-terminal: (y)es or (n)o
hungarian after apt-get install in gnome-terminal: (i)gen or (n)em
How to yes or no automatically in all languages? Not manual, not 'Y'/'I' or 'N'/'N' keydown.
I would like use this script my fresh installed Debian 7.1. I would like run this install.sh when Debian is installed for my all softwares when i would like use.
10 PC installing easy and faster my script.
If no script is slowly install for my 10 PC.
I would like to know if there is any way to detect a command if it going to be executed in the shell?
Eg:
Cmd: sudo apt-get clean I want a C program to be called before this cmd is executed.
Im having problems with this little thing in a C example Im doing. I want to show in the shell like this example:
Name Birthday
Tomas 13
Adrian 24
Nate 15
Im trying to use the printf function but the names doesnt have the same lenght. So I cant do it like this:
printf("Name Bithday
");
printf("%s %d
");
I'm running into a problem when I try to set a variable to an awk output in c-shell. Right now my command is Code: set STR_MSG_TYPE = `awk -F{ '/msg_type/ {print $2}' <filename> | tr -d }'/''*' ` I then run echo to see what the output is and it returns blank, however, when I run the same awk command from the command line, I get an actual output of "MT-715". Am I setting my variable incorrectly? I do something similar using the date command to set a STR_DATE variable earlier in the code and it works fine and I use the same syntax.
View 1 Replies View RelatedI want to spawn a GDB session from bash script and keep it working to interact it from outside. But when I start it in background (with '&' sigil).
Code: gdb -x gdb_script.txt ./a.out < gdb_pipe.fifo &
It executes passed script and terminates by itself (I do not pass any data or signals to it)
Code: Breakpoint 33, main (argc=1, argv=0xbffff0f4) at main.c:53
---Type <return> to continue, or q <return> to quit---53
read_main_config();
(gdb) quit
A debugging session is active.
Inferior 1 [process 5957] will be killed.
Quit anyway? (y or n) [answered Y; input not from terminal] What can I do to make it alive?
I am aware this has probably been covered hundreds of times, so apologies if so. I am fairly new to the linux scripting scene, so again apologies if what I'm saying seems pretty odd and makes no sense. I am attempting to write a script for some Linux Fedora test servers I have set up. For me to change which domain I have this set to point to, I would have previously changed the HOSTS and profile files manually, however I managed to make a script which changes these easily with the use of one command to launch the script.The problem I'm having is getting it to use the "export" command. I am aware this would have to be launched in the parent shell rather than the child and so I made a seperate script which has "export SIP_DOMAIN="test.blah.net" in it and had the first script "source" it. This doesn't work and I've probably done something somewhere that is incredibly stupid.
View 2 Replies View RelatedI am very new to shell script, and my requirement is --
1. open the apache access log, use "cut" and "grep" to find the numbers.
2.put the result in a file
3.then compare the same result with day before result
4. send the result via e-mail.
so how can i do that , using some functions..
I am trying to do some shell programs. I tried some sites regarding the while loop, they give the structure as:
Code:
while [ n1 -lt 500 ]
do
echo $((n1+100))
done
But the below code also worked for me:
Code:
while ((n1 > 500))
do
echo $((n1+100))
done
By using (( )) I could use while, for. But the documentations didnt follow this way. I mainly use this for datastructure programming.