I don't know too much about scripts but I need a script that will run this command:
g++ -O0 -c fileName.cpp && nm fileName.o | egrep ' [A-Z] ' | egrep -v ' [UTV] ' | grep -v .eh > fileName.txt
for files with names 000000 - 008577. So how should my script be written?
UPD2:
I've written a script and it works:
#!/bin/bash
s1="g++ -O0 -c "
s2=".cpp && nm "
s3=".o | egrep ' [A-Z] ' | egrep -v ' [UTV] ' | grep -v .eh >> "
s4=".txt"
for ((i=0; i<=8577; i++)) do
num="$( printf '%06d' ${i})"
s="${s1}${num}${s2}${num}${s3}${num}${s4}"
eval $s
done
Try removing the spaces around the equal sign when you get to num and s.
I've written working code:
#!/bin/bash
s1="g++ -O0 -c "
s2=".cpp && nm "
s3=".o | egrep ' [A-Z] ' | egrep -v ' [UTV] ' | grep -v .eh >> "
s4=".txt"
for ((i=0; i<=8577; i++)) do
num="$( printf '%06d' ${i})"
s="${s1}${num}${s2}${num}${s3}${num}${s4}"
eval $s
done
Related
I'm extracting two md5sums by using this code:
md5sum test{1,2} | cut -d' ' -f1-2
I'm receiving two md5sums as in example below:
02eace9cb4b99519b49d50b3e44ecebc
d8e8fca2dc0f896fd7cb4cb0031ba249
Afterwards I'm not sure how to compare them. I have tried using the xargs:
md5sum test{1,2} | cut -d' ' -f1-2 | xargs bash -c '$0 == $1'
However, it tries to execute md5sum as a command
Any advice?
Try using a command subsitution instead
#!/bin/bash
echo 1 > file_a
echo 2 > file_b
echo 1 > file_c
file1=file_a
# try doing "file2=file_b" as well
file2=file_c
if [[ $(sha1sum $file1 | cut -d ' ' -f1-2) = $(sha1sum $file2 | cut -d ' ' -f1-2) ]]; then
echo same
else
echo different
fi
I have a file like so:
- ${VAR1}/blah/blah:/blah1
- ${VAR2}/blah/blah:/blah2
- $VAR3:/blah3
I ultimately need to create those three folders.
I am using sed to extract the folder part:
$ cat test.txt | grep -E '^ +- \$.*?:.*?$' | sed 's/.*- \(\$.*\):.*/\1/g'
${VAR1}/blah/blah
${VAR2}/blah/blah
$VAR3
I need to create those folders but I need those shell variables to expand. Right now they don't:
$ cat test.txt | grep -E '^ +- \$.*?:.*?$' | sed 's/.*- \(\$.*\):.*/\1/g' | while read line; do echo "$line"; done
${VAR1}/blah/blah
${VAR2}/blah/blah
$VAR3
Is there a way to get the expanded strings so I can run mkdir instead of echo to make the folders?
You may use this bash script with envsubst:
#!/usr/bin/env bash
export VAR1 VAR2 VAR3
while IFS=' -:' read -r _ d _; do
mkdir -p "$d"
done < <(envsubst < test.txt)
Alternatively use this envsubst + awk + xargs solution:
envsubst < text.txt |
awk -F '[-:[:blank:]]+' -v ORS='\0' '{print $2}' |
xargs -0 mkdir -p
First of all those variables should be exported to be accessible from your script. Then you could just use the cut and tr commands combination to extract dir name in a loop like the following:
#!/bin/bash -eu
while read -r LINE; do
echo "$LINE" | cut -d ':' -f 1 | tr -d ' ' | tr -d '-'
done < test.txt
I'm not getting the result expect from the following code:
#!/bin/bash
cat /home/opmeitle/html/fiesta-one.html | grep -oiE '([$][0-9.]{1,7})'
this is output:
$90.850
$0
$389
$469
$670
$750
$684
$21.744
$604
Here is the result I desire, in console.
$90.850 $0 $389 $469 $670 $750 $684 $21.744 $604
I appreciate your answers. thanks
luis.
There are many other solutions:
$ echo $(grep -oiE '([$][0-9.]{1,7})' /home/opmeitle/html/fiesta-one.html)
$ grep -oiE '([$][0-9.]{1,7})' /home/opmeitle/html/fiesta-one.html | xargs echo
$ grep -oiE '([$][0-9.]{1,7})' /home/opmeitle/html/fiesta-one.html | tr '\n' ' '
$ grep -oiE '([$][0-9.]{1,7})' /home/opmeitle/html/fiesta-one.html | perl -pe 's/\n/ /;'
And without grep:
$ perl -ne 'print "$1 " if /([\$][0-9.]{1,7})/' /home/opmeitle/html/fiesta-one.html
A simple fix would be to translate the newlines into spaces (I also removed your unnecessary use of cat):
grep -oiE '([$][0-9.]{1,7})' /home/opmeitle/html/fiesta-one.html | tr '\n' ' '
I wrote the following code as part of my project work. I need to print i value based on the number of ;(semi colons) in the input string. But the while loop is not getting executed. It is returning errors. I tried lot of alternatives but could not figure it out.
IN="aa;bb;cc;"
c= echo $IN | tr -dc ';' | wc -c
echo $c
i=1
while [ $i -le $c ];
do
echo $i
i=`expr $i + 1`
done
You need to change this:
c= echo $IN | tr -dc ';' | wc -c
to this:
c=`echo $IN | tr -dc ';' | wc -c`
so that echo $IN | tr -dc ';' | wc -c is run, and its output saved in c — just like you're already doing for i later in the script:
i=`expr $i + 1`
Well if you can use awk, it's much shorter and easier:
echo $IN | awk '{ print length(gensub("[^;]","","g",$0)) }'
There's no need for looping once you have a string of ;. The shell can count the characters in a variable for you with ${#variable}.
$ IN="aa;bb;cc;"
$ c=$(echo $IN | tr -dc ';')
$ echo ${#c}
3
Hey every one! I am pretty new for shell script and I am stuck
I need to extract information regarding: file_name && size && time && row_count and I want it do in one command line. I tried like this :
ls -l * && wc -l file.txt && du -ks file.txt | cut -f1| awk '{print $5" " $6 " " $7 " "$8 " " $9 " "$1 " "$2}'
but is not working properly
I also tried do in loop but i dont know how extract from there
for file in `ls -ltr /export/home/oracle/dbascripts/scripts`
do
[[ -f $file ]] && echo $file | awk '{print $3}'
done
Then I want to redirect to file like this >> for sql loader purpose.
Thanks in advance!
This could be a start if you have GNU find and GNU coreutils (most Linux distribution will do):
for i in /my/path/*; do
find "$i" ! -type d -printf '%p %TY-%Tm-%Td %TH:%TM:%TS %s '
wc -l <"$i"
done
/my/path/* should be modified to reflect the files you want to probe.
Also keep in mind that this one-liner has a few major issues if any directories are specified. This should be safer in that regard:
for i in *; do
if [[ -d "$i" ]]; then
continue
fi
find "$i" -printf '%p %TY-%Tm-%Td %TH:%TM:%TS %s '
wc -l <"$i"
done
You will want to see the manual page for GNU find to understand this better.
EDIT:
There is at least other faster way, using join and bash process substitution, but it's a bit ugly and somewhat harder to make safe and work the kinks out of.
ExtractInformation()
{
timesep="-"
sep="|"
dot=":"
sec="00"
lcount=`wc -l < $fname`
modf_time=`ls -l $fname`
f_size=`echo $modf_time | awk '{print $5}'`
time_month=`echo $modf_time | awk '{print $6}'`
time_day=`echo $modf_time | awk '{print $7}'`
time_hrmin=`echo $modf_time | awk '{print $8}'`
time_hr=`echo $time_hrmin | cut -d ':' -f1`
time_min=`echo $time_hrmin | cut -d ':' -f2`
time_year=`date '+%Y'`
time_param="DD-MON-YYYY HH24:MI:SS"
time_date=$time_day$timesep$time_month$timesep$time_year" "$time_hrmin$dot$sec
result=$fname$sep$time_date$sep$f_size$sep$lcount$sep$time_param
sqlresult=`echo $result | awk '{FS = "|" ;q=sprintf("%c", 39); print "INSERT INTO SIP_ICMS_FILE_T(f_name, f_date_time,f_size,f_row_count) VALUES (" q $1 q ", TO_DATE("q $2 q,q $5 q "),"$3","$4");";}'`
echo $sqlresult>>data.sql
echo "Reading data....."
}
UploadData()
{
#ss=`sqlplus -s a/a#adb #data.sql
#set serveroutput on
#set feedback off
#set echo off`
echo "loading with sql Loader....."
}
f_data=data.sql
[[ -f $f_data ]] && rm data.sql
for fname in * ;
do
if [[ -f $fname ]] then
ExtractInformation
fi
UploadData
#Zipdata
done