Have bash echo the final form of executed command, can I? - bash

I have a small bash script build1c.sh .
if [ "$1" = "" ]; then
echo "You must give a .c file to compile."
exit 1
fi
cfile=$1
stem=${cfile%.*}
set -o verbose
gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto
set +o verbose # optional here
The intention is to only echo the gcc commands being executed. I work to some extend. When I call build1c.sh client2.c , I see output
gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto
set +o verbose # optional here
Still wacky, right? Those var reference($cfile, $stem) do not get their final form, so the echoing becomes pretty useless.
You know, what I like to see is
gcc -c -g -Wall client2.c
gcc -o client2 client2.o common.o reentrant.o -lssl -lcrypto
Is there correct and concise way to address this?
BTW: Minor request: Can I suppress the echoing of set +o verbose itself?

Replace set -o verbose with set -x

function echo_and_execute {
echo "$#"
"$#"
}
echo_and_execute gcc -c -g -Wall $cfile
echo_and_execute gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

Related

Shell script not running through all files of certain extensions

I wrote a shell script that searches the directory that it is in for files that match any suffixes in the env var SUFFIXES.
#!/bin/bash
SUFFIXES=$(SUFFIXES:-'.c .cpp .cc'}
CC=${CC:-gcc}
CFLAGS=${CFLAGS:-"-Wall -Werror"}
VERBOSE=${VERBOSE:-1}
for i in *$SUFFIXES
do
$CC $CLAGS -o ${i%$SUFFIXES} $i 2> /dev/null
if [ $VERBOSE == 1 ]
then
echo "$CC $CLAGS -o ${i%$SUFFIXES} $i"
fi
done
Say I have a directory with hello.c, hey.c, howdy.cc, and hallo.cpp. Whenever the script is ran without changing the environment variables in the terminal, I get the following output:
gcc -Wall -Werror -o hello.c hello.c
gcc -Wall -Werror -o hey.c hey.c
gcc -Wall -Werror -o .cc .cc
gcc -Wall -Werror -o .cpp .cpp
The output that I am trying to get is:
gcc -Wall -Werror -o hello hello.c
gcc -Wall -Werror -o hey hey.c
gcc -Wall -Werror -o howdy howdy.cc
gcc -Wall -Werror -o hallo hallo.cpp
How would I go about fixing this?
Except for a few typos in your code that probably occurred here, you have some mistake in your loop
i in *$SUFFIXES means that *.c .cpp .cc, so it won't work on file.cpp or file .cc
${i%$SUFFIXES} try to cut all suffixes that you defined, from end of file name, while you have one of them in each file name
maybe an inner loop will helpful
for i in $SUFFIXES
do
for j in *$i
do
$CC $CFLAGS -o ${j%$i} $j 2> /dev/null
if [ $VERBOSE == 1 ]
then
echo "$CC $CFLAGS -o ${j%$i} $j"
fi
done
done

Same Makefile executing different commands in different computers

During installation of pintos, I had to run make.
Following is the Makefile.
all: setitimer-helper squish-pty squish-unix
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
In one computer it executed correctly. (output for the command is given below)
gcc -Wall -W -c -o setitimer-helper.o setitimer-helper.c
gcc -lm setitimer-helper.o -o setitimer-helper
gcc -Wall -W -c -o squish-pty.o squish-pty.c
gcc -lm squish-pty.o -o squish-pty
gcc -Wall -W -c -o squish-unix.o squish-unix.c
gcc -lm squish-unix.o -o squish-unix
but in other computer I got the following error
gcc -lm setitimer-helper.o -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xc9): undefined reference to `floor'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'setitimer-helper' failed
make: *** [setitimer-helper] Error 1
If looked at first line of outputs of both make commands
gcc -Wall -W -c -o setitimer-helper.o setitimer-helper.c
and
gcc -lm setitimer-helper.o -o setitimer-helper
They are different.
Why make is executing different commands for the same Makefile? and What should I do to remove error?
In the first computer, the setitimer-helper.o file either doesn't exist or the setitimer-helper.c file is newer, so make needs to rebuild it. Thus it runs the compiler, then afterwards it performs the link operation:
gcc -Wall -W -c -o setitimer-helper.o setitimer-helper.c
gcc -lm setitimer-helper.o -o setitimer-helper
On the second computer, the setitimer-helper.o file already exists and is newer than the setitimer-helper.c file, so the compile command was not needed and the second computer proceeded directly to the link line:
gcc -lm setitimer-helper.o -o setitimer-helper
The real question is why you got the linker error on the second computer.
The answer to that is that the -lm flag needs to come on the linker line after the object files. This happens because you added -lm to the LDFLAGS variable which is not the right one: that should contain options that tell the linker where to look for files, etc. (for example, the -L option).
Libraries should be added to the LDLIBS variable, not LDFLAGS. Change your makefile to this:
all: setitimer-helper squish-pty squish-unix
CC = gcc
CFLAGS = -Wall -W
LDLIBS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
Your link line will then look like:
gcc setitimer-helper.o -o setitimer-helper -lm
and should work properly.

Script for compiling C++

I want to make a script that would make compiling things quicker to write. I want to compile a XYZ.cpp file and the I want the output to be XYZ.out.
Here's my script:
#!/bin/bash
# getting the part before .cpp
var1=`echo "$1" | cut -d"." -f1`
# compile
g++ -std=c++11 -Wall -pedantic -Wno-long-long "$1" -o "$var1.out" -lm
I run it: ccc program.cpp
The script runs but the output still is a.out. What am I doing wrong?
Just use one of bash's built-in parameter expansion features to remove any trailing portion of $1 that matches ".cpp":
#!/bin/bash
g++ -std=c++11 -Wall -pedantic -Wno-long-long -o "${1%%.cpp}.out" -lm "$1"
I've also moved the source file to be the last parameter to g++, so that it's clear where the options are (and end).

How to create makefile for compiling multiple programs

I have some problems with creating makefile. I have tried to compile some files from terminal and found right options for gcc to do this.
But I have some troubles with creating one makefile to do all this tasks.
Here are my commands that work.
gcc -c pstree.c -DHAVE_CONFIG_H -I.
gcc -o hello pstree.o -L/usr/lib/ -ltinfo
gcc -c fuser.c -DHAVE_CONFIG_H -I.
gcc -c -o libsignals.a signals.c
gcc -o hello fuser.o -L/usr/lib/ -L. -lsignals
gcc -c -o libsignals.a signals.c
gcc -o hello killall.o -L/usr/lib/ -L. -lsignals
gcc -c killall.c -DHAVE_CONFIG_H -I.
How can I make a Makefile for executing these commands?
killall: hello\ fuser.o
gcc -c -o libsignals.a signals.c
gcc -o hello killall.o -L/usr/lib/ -L. -lsignals
gcc -c killall.c -DHAVE_CONFIG_H -I.
fuser: hello\ pstree.o
gcc -c fuser.c -DHAVE_CONFIG_H -I.
gcc -c -o libsignals.a signals.c
gcc -o hello\ fuser.o -L/usr/lib/ -L. -lsignals
pstree:
gcc -c pstree.c -DHAVE_CONFIG_H -I.
gcc -o hello\ pstree.o -L/usr/lib/ -ltinfo
clean:
rm -rf *.o
I wrote very very dirty you can define cc variable such as:
CC = gcc
CFLAGS = -g -Wall -I.
LIBS = -L/usr/lib/ -lsignals
and so on.....
Mine is very dirty ....
NOTE: Before each line of intending you have to use TAB, not space.
usage :
make clean
make killall

Programming in Lua on OS X?

What can I use to program Lua script on Mac OS X? I'm looking for something that I can use to compile/interpret Lua script on OS X.
My preferred way:
brew install lua
Thanks, Max!
And if you need to know how to install Homebrew, see Link and:
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
The Lua source easily compiles with no changes on the mac. It will build lua (the interpreter which can act on a source script, a pre-compiled script or interactively) and luac which can be used to pre-compile source scripts.
From the lua.org website: http://luabinaries.luaforge.net/download.html. The ones you want are the darwin binaries (they say Mac OS X in the description).
My favorite way (from the shell):
sudo port install lua
I LOVE macports!
Here is my terminal session from compiling and installing Lua from source, basically following these directions. I already had Apple's Developer Tools installed, and /usr/local/bin was already in my PATH, so I was able to skip some of the more time-consuming and/or tedious steps in the directions.
$ cd ~/Downloads
$ tar -xf lua-5.1.4.tar
$ cd lua-5.1.4
$ make macosx
cd src && make macosx
make all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lapi.o lapi.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lcode.o lcode.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ldebug.o ldebug.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ldo.o ldo.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ldump.o ldump.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lfunc.o lfunc.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lgc.o lgc.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o llex.o llex.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lmem.o lmem.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lobject.o lobject.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lopcodes.o lopcodes.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lparser.o lparser.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lstate.o lstate.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lstring.o lstring.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ltable.o ltable.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ltm.o ltm.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lundump.o lundump.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lvm.o lvm.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lzio.o lzio.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lauxlib.o lauxlib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lbaselib.o lbaselib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ldblib.o ldblib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o liolib.o liolib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lmathlib.o lmathlib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o loslib.o loslib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o ltablib.o ltablib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lstrlib.o lstrlib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o loadlib.o loadlib.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o
ranlib liblua.a
gcc -O2 -Wall -DLUA_USE_LINUX -c -o lua.o lua.c
gcc -o lua lua.o liblua.a -lm -lreadline
gcc -O2 -Wall -DLUA_USE_LINUX -c -o luac.o luac.c
gcc -O2 -Wall -DLUA_USE_LINUX -c -o print.o print.c
gcc -o luac luac.o print.o liblua.a -lm -lreadline
$ make test
src/lua test/hello.lua
Hello world, from Lua 5.1!
$ sudo make install INSTALL_TOP=/usr/local
Password:
cd src && mkdir -p /usr/local/bin /usr/local/include /usr/local/lib /usr/local/man/man1 /usr/local/share/lua/5.1 /usr/local/lib/lua/5.1
cd src && install -p -m 0755 lua luac /usr/local/bin
cd src && install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp /usr/local/include
cd src && install -p -m 0644 liblua.a /usr/local/lib
cd doc && install -p -m 0644 lua.1 luac.1 /usr/local/man/man1
$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print "Hi"
Hi
> = 2 + 3
5
> ^c
$ cd test
$ lua factorial.lua
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
If you don't want to compile your own Lua binaries, you can try ZeroBrane Studio Lua IDE, which comes packaged as a .dmg file for OSX. It's an IDE that allows you to edit and debug your Lua scripts. If you are just starting with Lua, it also includes 50+ examples and demo scripts, as well as integrated instructions on running them, so you won't be facing an empty screen not knowing where to start.
I just recently found Rudix—a maintained collection of precompiled Unix software for Mac.
While I'm sure you've already figured out a way of installing Lua, I came across your question by Googling the same thing. For anyone that's interested, here's the link to a recent Lua 5.1.4 dmg.

Resources